Select With Multiple Conditions
I've a table called tag_log.
In this table are two columns called tag_id and sub_id. This table is used to link tag names to stories.
I'm trying to write the query for a search. The search is looking for all the sub_id that matches the tag_id given.
Example of what I have currently:
select distinct(sub_id) from tag_log where tag_id in(101, 102, 103);
In the above example, it was performing a union instead of what I really want. It was returning results that have either one or all of the tags.
What's required is for the story to have ALL the three tags. It's should be like a typical search, where the more search terms you enter, the more focused the results will be.
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Need Help With Multiple WHERE Conditions...
i'm wondering if anyone knows how to have multiple WHERE conditions...i'm using php/mysql as my programing languages... example --> SELECT * FROM table WHERE rid=$ride && uid=$userid && accepted=Ɔ' I need help selecting rows where all three of the conditions above are met...anyone knows how to do this?
Specify Conditions For The Left Side Of A Multiple Left Join
I'm doing a left join that looks like this standard example: SELECT t1.c1, t2.c2, SUM(t3.c3) FROM t1 LEFT JOIN t2 ON t1.c1=t2.c1 LEFT JOIN t3 ON t1.c1=t3.c1 GROUP BY t1.c1 The problem is that I also want to specify a condition for selecting records from t1: WHERE t1.c1='x' so that only the records with that value in c1 will be returned on the left side of the join. I don't know where to put this in the SQL.
Select Statement With Multiple Counts/multiple Joins Trouble
SQL SELECT g.group_id, g.name, g.description, g.icon, g.user_id, g.date, u.username, count(c.comment_id) as comment_count, count(v.video_id) as video_count, count(m.user_id) as user_count FROM duo_groups as g LEFT JOIN duo_group_members as m on m.group_id=g.group_id LEFT JOIN duo_users as u on u.user_id=m.user_id LEFT JOIN duo_videos as v ON g.group_id=v.group_id LEFT JOIN duo_video_comments as c on v.video_id=c.video_id GROUP BY g.group_id, g.name, g.description, g.icon, g.user_id, g.date, u.username Will return My current problem with the above statement is with the counts. The count is accurate when only one of the counts is returned. However when more than 1 of the counts is returned the other counts (if they are not 0) will return the same number. For example look at the screenshot above. The first row, all three counts are the same but they are not accurate. They alll point to 8 because there are 8 comments, but there aren't 8 videos or 8 users. Same with the second row. There aren't 2 users only 2 videos.
Select / Insert Multiple Rows As A Single Row Of Multiple Columns
I have a nice database set up that contains information about orders and the items on those orders. If an order has 10 items on it, I can select the item data which returns 10 rows of data (let's say 5 colums each). Beautiful! Now I find myself needing to satisfy a program that requires all of the data on a single row. I can do this in a higher level language, but if I could accomplish it all in mysql it would be better. I don't need to sum or do any calculations. I just want to select those 5 columns of data about those 10 rows worth of items as a single row with 50 columns. For example, I'd want this: 1-1,1-2,1-3,1-4,1-5 2-1,2-2,2-3,2-4,2-5 To become: 1-1,1-2,1-3,1-4,1-5,2-1,2-2,2-3,2-4,2-5 The first complication is that the number of items on an order is variable, but is always at least 1 and can not exceed 20. The closest I've been able to get is to do something like: SELECT GROUP_CONCAT(item_number,",",qty,","",description,"",",price,",",location_number SEPARATOR ",") FROM items WHERE order_number=12345 This will give me a single text string containing the value content of the INSERT query (which will need to be manipuated outside of the SQL query to pad it with NULL values for the unused items' columns etc).
Where Conditions
I am using MySQL phpmyadmin on my server. I would like some examples of different conditional codes to retrieve info from my tables.
LIKE Conditions
Basically I have a table, with a column which holds a list of related tags separated by spaces. The user can search for a row using a key word that will search this column (and the column named header too) and return if the keyword appears in the tags (or heading)... I tried to achieve this by using the following SQL but it actually returns every entry in the database... I have never used "LIKE" before so if someone could give me a slap and say I am doing something stupid that would be awesome . SELECT * FROM VIDEOS WHERE VIDEO_HEADER LIKE '%$seachText%' OR VIDEO_TAGS LIKE '%$searchText%'
WHERE Conditions
I'm trying to optimize a search and I'm wondering if anybody knew whether the order of the conditions in the WHERE clause actually matters. Is it parsed from left to right? If so, then should I put the conditions likely to eliminate the most records first, followed by those that will eliminate the least? Also - I need to join several tables for this search (1 master table that contains user info and about 7 secondary tables that include additional information on users) and I am trying to decide whether I should do the joins one at a time and eliminate users in a series of steps (to prevent getting an overly large table), or if I should evaluate and combine everything in a single statement. The problem is I don't really know what the parsing order of the SELECT statement is.
2 Of 5 Conditions Match
I need my query to match at least 2 of 5 given conditions. Example: I have conditions content REGEXP 'A' content REGEXP 'B' content REGEXP 'C' content REGEXP 'D' content REGEXP 'E' I need database to return this entry only if at least 2 conditons are true.
MySQL 5.0 And OR Conditions
I have heard that version 5.0 of MySQL has performance gains for "OR" conditions. Can anyone confirm this? If so, how significant are the performance gains over 4.1? For instance, I am referring to something like: SELECT * FROM some_table WHERE this_thing = 'something' OR that_thing = 'whatever';
Search On Several Conditions
I want to retreive data from a table on several conditions in order; for example; All i am trying to do is select all records on the following condition; User_id = variable AND (Profile = "variable" OR Profile = "Both") ORDER by hits in DESC I came up with this statement but no matter what i do i cannot get it to produce the result above "SELECT * FROM links WHERE user_id='$user_id' AND (profile='$mycurrentprofile' OR profile='Both') GROUP BY total_hits DESC" Can anyone offer me any help please, would really appreciate this as its doing my head in. BTW, the following statement seems to work so not sure whats going on ? Only thing i can think is that its screwing up if total_hits above is equal 0 ? "SELECT * FROM links WHERE folder_id='$activefolder' AND (profile='$mycurrentprofile' OR profile='Both') GROUP BY title ASC"
MySQL 5.0 And OR Conditions
I have heard that version 5.0 of MySQL has performance gains for "OR" conditions. Can anyone confirm this? If so, how significant are the performance gains over 4.1? For instance, I am referring to something like: SELECT * FROM some_table WHERE this_thing = 'something' OR that_thing = 'whatever';
Sums For Two Different Conditions
I want to combine the following 2 queries into 1 query where it would give me 2 results (proflad and proftot) but don't know how select sum(return-inv)as proflad from allbets where com="lad" select sum(return-inv)as proftot from allbets where com="tot"
Transactions And Race Conditions
In an application I'm developing, I'm at a point where there needs to be tight integration of application-level logic sprinkled in between several consecutive SQL statements. This negates the option to use stored procedures since subsequent SQL statements depend on application logic that cannot be transferred into the database. I need the ability to rollback on errors (i.e. I need to use a transaction), but I also need to avoid race conditions. Reading the MySQL documentation, START TRANSACTION will implicitly issue an UNLOCK TABLES statement; conversely, LOCK TABLES will implicitly COMMIT any active transaction. At this point it appears that I have to choose between transactions and locks, but I need the functionality of both.
Two Alike Conditions In Query?
Example. I have a table with information about payments (id, amount, pay_date, customer_id). Can I select two sums for two different date ranges, grouped by 'customer_id' (for each customer) in one query?
Left Join Conditions
Two tables. table1, table2 Table 1 contains all unique items. Table 2 contains x references to table 1, unique by customerId I want to do a join onto table2 where table1.id = table2.table1Id AND table2.customerId = the user's ID I can't quite figure out how to work in the idea of an AND into my left join. Unless both those conditions are two, I don't want any data from table2. Only solution I can think of so far is to just left join on the id's, but select the customerId from table2, and as I loop through, check to see if it's supplied and the right id, and keep track of duplicate items from multiple customers myself. Suggestions?
Conditions On Left Join
I'm having trouble constructing this left join query. I want to retrieve all rows from tProducts, and relevant matches from pictures, however what I have got just jams up the server and I have to restart. Am I doing it wrong? SELECT prodCode, prodName FROM tProducts LEFT JOIN pictures ON productID = relProdId AND prodCode = 'SF2'
Conditions In UPDATE Statement
i have some data to be updated in a specific row - WHERE id=1 data: $min_time, $max_time, $time, $attempts so, i would like to calculate new avg and replace min_time with new value, if new min_time is smaller then that one stored in the database. "UPDATE mytable SET avg=(avg*attempts+$time) DEV (attempts+1), min_time=**here i could use help** ??IF(min_time>$min_time)$min_time ELSE min_time?? **and similar with max_time .
Query Performance On Column Conditions
I have a question regarding MySQL performance on column conditions. How MySQL reacts where you add a condition to the same column? For example: SELECT * FROM users WHERE status<>4 and the same query with extended column check SELECT * FROM users WHERE status<>4 AND status<>5 ... I am specially interested in situation where the conditions checks a column value and whether the data is specified: SELECT * FROM users WHERE status<>4 OR status IS NULL What's with the MySQL's performance in such cases? Is the same column (status in the above example) checked twice or the optimizer takes care of it in some special way?
OUTER JOIN With Extra Conditions?
I need to get a list of products in a certain category, along with the quantity of each item already added to the shopping cart for a given session number. I'm having a heck of a time satisfying the latter condition. shoppingCart table: +--------+-----------+-----------+-----+ | cartID | sessionID | productID | qty | +--------+-----------+-----------+-----+ product table: +-----------+--------------+------+--------+ | ProductID | ProductCatID | name | Active | +-----------+--------------+------+--------+ Here's my base query, which just gets all the products in a given category. SELECT product.* FROM product WHERE `ProductCatID`='{$id}' AND `Active`=Ƈ' Here's the query I've got so far: SELECT product.*, shoppingCart.qty FROM product LEFT OUTER JOIN shoppingCart ON shoppingCart.productID = product.ProductID WHERE `ProductCatID` = Ɗ' AND shoppingCart.sessionID = '{$sessionID}' AND `Active` = Ƈ' Obviously, this does NOT work, because it limits the query to ONLY products that have the specified sessionID. I need it to return ALL the products in the category, but give me the quanity for items in the shoppingCart table, ONLY IF the sessionID matches (otherwise it should return NULL)!
Multiple Select
I've the following quries, can I changed these to one query? PHP SELECT heading as next_page FROM table WHERE id=10 SELECT heading as previous_page FROM table WHERE id=8 SELECT heading as contents_page FROM table WHERE id=1
How-to Multiple Select
I'm studying a blog program. Given two tables : post and comment, i would like to retreve the number of comments for each post. In postgres the following is doable : select id, title, body, (select count(*) from comment where post_id = post.id) as total_comment from post How-to do the same thing with MySQL?
Combining UPDATE Statements With Different Fields And Conditions
I'm slightly paranoid that I haven't unearthed an existing answer to this question, but the ones I have been able to find didn't have two different variables requiring two different conditions. Basically, I want to know if it's possible to combine the following UPDATE statements into a single query: UPDATE table SET field1 = field1 - n WHERE field 1 > x UPDATE table SET field2 = field2 - n WHERE field 2 > x .......
Multiple Condition Select
I have an issue with a select that I needed a little help with. I have the following table structure with some sample data: user_id, group_id, type 11, 1, 1 11, 2, 1 11, 3, 2 11, 4, 3 11, 6, 4 12, 1, 1 13, 3, 2 14, 1, 5 15, 3, 6 What I need to do is select all of the user_id's that have a group_id record of 1 AND 6. I tried a couple of things but I want to make sure I am doing this correctly. Also, if this is not the correct forum, can someone point me to the correct one? I could not find anything for SQL syntax.
Multiple Select And A Formula
So i have a problem, i run into a more complicate query, at least for me is complicate. I have more tables but in my example will be just 3. users (id,nick) comm (id,userID) rev (id,userID) In fact, i need to calculate from the query directly the users points, Each user receive a number of points for each comm, rev he has... For a single user it`s simple, i made the maths in php, but now i need a user TOP. user points = COUNT(comm.id)*x + COUNT(rev.id)*y I need to count how many comm and rev has every user, that`s easy. Next i need them to multiply each one by a number, i used count()*x, is easy. The problem begins now, how i make a variable user points, I start something but it desn`t work fine, SELECT COUNT(stiri_comm.id) AS 'nrComm', COUNT(mmorpg_reviews.id) AS 'nrRev' ,users.* from stiri_comm,mmorpg_reviews,users WHERE users.id = stiri_comm.userID AND users.id = $id AND stiri_comm.ok=1 AND users.id = mmorpg_reviews.userID AND mmorpg_reviews.ok=1 GROUP BY users.nick; the result is : nrComm = nrRev = max (nrRev,nrComm) and it`s wrong, i don`t know why.
Select Sum(x) >0 With Multiple Columns
I am writing a script to show the average value of a question in a submitted survey form. I have 14 questions, which can have the value 1-10, if the user submitts "I don't know" it returns the value 0. When i calculate the average it includes the value 0 but still divides with all the submitted answers (see code). How can i make a select statement which only includes values > 0? Current select statement: $select = "SELECT SUM(S1), SUM(S2), SUM(S3), SUM(S4), SUM(S5), SUM(S6) , SUM(S7), SUM(S8), SUM(S9), SUM(S10), SUM(S11) , SUM(S12), SUM(S13), SUM(S14) FROM Evaluering"; I want it to do something like this : $select = "SELECT SUM(S1), SUM(S2), SUM(S3), SUM(S4), SUM(S5), SUM(S6) , SUM(S7), SUM(S8), SUM(S9), SUM(S10), SUM(S11) , SUM(S12), SUM(S13), SUM(S14) WHERE S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14 > 0 FROM Evaluering"; Any suggestions?
Select With Multiple Tables
I have a select coded like: "SELECT * FROM Table1, Table2, Table3 WHERE Key1='Joesmith'" the query works correctly except that both Table1 and Table2 has a column named "Points" What is the proper way to retrieve the correct column named Points? I tried "Table1.Points and that failed. I changed the order of table names and then I retrieved the one I wanted (ie. Table2). It appears that if two tables have the same colun name that the one referenced is the last one listed.
Multiple WHERE Statements In SELECT
How can I have more than one WHERE clause, like this: SELECT DISTINCT email FROM ehousebo WHERE waterway="1000 Islands" OR waterway="Alum Creek Lake" I tried using HAVING, but that gave an "unknown column" error. SELECT DISTINCT email FROM ehousebo HAVING waterway="1000 Islands" OR waterway="Alum Creek Lake" Invalid query: Unknown column 'waterway' in 'having clause'
Multiple Database Select
How do I at the best way perform select statements over multiple databases? I have a couple of databases containing the same table definitions with diffrent data. Now I want them to act as a single database giving me one answer on a select statement but the answer fetched from all my defined databases. Right now the databases are on the same node and I guess it is possible to do something like: select * from db1.table1, db2.table1 etc etc. but I really do not want to modify all my sql statements if another database would comes into picture or is revoved from the set.
Multiple Row Select Issue
Why won't this work? I just get blank page. $result = mysql_query("SELECT $filt FROM charts WHERE vmanuf IN ('$manuf') ORDER BY $order") or die (mysql_error()); where $manuf = 'string1, string2, etc...' (when the string is one, it works).
Select Multiple Rows With The Same Id
So I have this table: table_name id_one ---- id_two 1 ---------- 1 1 ---------- 2 1 ---------- 3 2 ---------- 1 2 ---------- 3 I want to select all of the id_one rows where the id_two is '1' and '2'. The desired output is: id_one 1 1 Later I might use SELECT DISTINCT to produce the following: id_one 1 So far I have: SELECT id_one FROM table_name WHERE id_two IN ('1','2') But this selects all the id_one rows where id_two is '1' OR '2'. I want the rows where it is '1' AND '2'.
Select From Multiple Tables
I have multiple tables named like this wpctchie, wpctchee, wpcbasfr, wpcbasth etc. The idea is that admin will be able to search all tables beginning "wpc" (all tables), superusers will be a able to browse tables beginning "wpctch" ("wpctchie" and "wpctchee") and users will be able to search only one table e.g "wpctchie" Is it possible to do a search such as SELECT * FROM wpc% (for admin) SELECT * FROM wpctch% (superusers) SELECT * FROM wpctchie (well this just works anyway!) Does that make sense what i am trying to do?
Select From Multiple Tables
What is wrong with this query?Code: SELECT * FROM armor, etcitem, weapon WHERE armor.item_id = '57' AND weapon.item_id = '57' AND etcitem.item_id = '57';
Granting User Access On Field With Restrictions Or Conditions
I am planning to develop a database that will have many users. Assume I have a table called Customer having these fields (id, name, user). Assume we have these Data: ID Name User 1 Cust1 user1 2 Cust2 user1 3 Cust3 user1 4 Cust4 user2 5 Cust5 user2 I want the users to have access on those records that are created by them only or those that have their name on in the (User Field). For example, if (user1) accessed the database and called: "SELECT * FROM customer;" it should return these data only automatically (or throw an exception at least): Result: 1 Cust1 user1 2 Cust2 user1 3 Cust3 user1 (or throw an exception at least) that he cant call this statements without a where cluse like (USER=user1). also, the user should have only the right to insert records having their name on in the (User) Field. For example: user1 can only call : "INSERT in CUSTOMER Values (6 , Cust6, user1), if he inserts user2 instead, the system should through an exception. I hope I made clear examples of what I would like to build. How can I acheive this?
Select X Parents That May Have Multiple Childs
I'm playing with writing a product review system that allows multiple rating types per review (overall, cost, quality, etc.). When I display the reviews, I only want to display 5 reviews, but I don't know how to limit. I also want to be able to set up pagination for the reviews, so a preferred solution would allow me to say LIMIT 0, 5 or something. My table structure is similar to:
SELECT DISTINCT With Multiple Fields
I'm kind of stumped on a query I'm trying to build. basically I have a forum table like this: Post_ID | Thread_ID | Post_EntryDate | etc..... 90 | 22 | .... 89 | 21 | .... ... ... ... 85 | 1 | .... 84 | 1 | .... 83 | 10 |.... and so on. I would like to get the last 20 threads with most recent posts filtering for Thread_ID duplicates of course. so far I have this: SQLtemp = "SELECT DISTINCT Thread_ID FROM Posts ORDER BY Post_ID DESC LIMIT 0,20" this succesfully returs a list of unique Thread_IDs of the last 20 threads with most recent posts. However, I need to get the Post_IDs and other fields but when I throw Post_ID in the SELECT statement all I get is duplicate values for Thread_ID
Php/Mysql Multiple Database Select
I'm trying to SELECT from two Mysql Databases in one SELECT statement. Both databases are on the same server. I think I have the SELECT statement syntax correct. What I don't get, is how to connect to query multiple databases at once. I've been messing with the following: $db1 = @mysql_connect("localhost","db1","password"); $db2 = @mysql_connect("localhost","db2","password"); mysql_select_db("db1",$db1); mysql_select_db("db2",$db2); $query = "SELECT db1.field1, db2.field1 FROM db1.table1, db2.table1; $post_table = mysql_query($query); I don't have a strong understand of database connections,
Select Same Row Multiple Times Using Column
I've seen this asked before and although someone answered that it is possible, they did not post the solution, so if anyone can enlighten me that would be helpful. Basiclly with table info name | quantity john | 3 anne | 1 will | 2 Return results john john john anne will will
Select DISTINCT Between Multiple Columns??
So i have a table 'hobbies' in which i store up to 6 user's hobbies. The columns are called: hobby1 hobby2 hobby3 hobby4 hobby5 hobby6 Now I want to list all hobbies but without having the same results over and over. I tried: SELECT DISTINCT hobby1, hobby2, hobby3, hobby4, hobby5, hobby6 but this filters only hobby1 Also tried: SELECT DISTINCT hobby1, DISTINCT hobby2, DISTINCT hobby3, DISTINCT hobby4, DISTINCT hobby5, DISTINCT hobby6
Multiple COUNT() In SELECT Statement
SELECT COUNT(s01_Products.id) FROM s01_Products LEFT JOIN s01_Attributes ON s01_Attributes.product_id = s01_Products.id LEFT JOIN s01_Options ON s01_Options.attr_id = s01_Attributes.id WHERE ( s01_Products.active = 1 ) AND ( ( LEFT(s01_Options.prompt,5) = "Small" ) OR ( LEFT(s01_Options.prompt,2) IN (28,30) ) ) SELECT COUNT(s01_Products.id) FROM s01_Products LEFT JOIN s01_Attributes ON s01_Attributes.product_id = s01_Products.id LEFT JOIN s01_Options ON s01_Options.attr_id = s01_Attributes.id WHERE ( s01_Products.active = 1 ) AND ( ( LEFT(s01_Options.prompt,6) = "Medium" ) OR ( LEFT(s01_Options.prompt,2) IN (32) ) ) And one for Large and Extra Large... I would obviously like to combine all 4 queries in to 1 so that I can get my 4 counts in a single,
Multiple Table Joins In A Select
How do I write a select query to join more than two tables? =20 Table A relates to table B and table B relates to Table C. I need to return fields from Table A and C that are related..
Select Statement From Multiple Tables
how can I do this type of join: table1: id, value1, value2 table2: id, value1, value2 table3: id, value1, value2 the "id" is a product code the values are new & used pricing I need a select that can join all this data, and fill 0's if an id is not listed in a gven table, then output to a file like so: id, table1.value1, table1,value2, table2.value1, table2.value2, table3.value1, table3.value2 obviously, if I join on all the ids, it only will output the values that have the same id in every table. how do I get that plus all the values that are unique to each table?
SELECT With Multiple WHERE Statments (using An Index)
I have a 35000 record database relating to geneology, about 50 fields per record, most null at any given time, almost all the fields are in TINYTEXT format. I have a php frontend with two different search options. One that allows you to search 5 fields at once and one that allows you to search everything. So on my homepage you can search by surname (tinytext) firstname (tinytext) sex (1 char) date (2 tinyint) location (tinytext) arrival port (tiny text) On an advanced search page you can search by all 50 fields selectively. When the SELECT is run, it basically checks which form field is filled in and if it is, then attach an AND statement to the end of the query. The initial query is "WHERE recid IS NOT NULL" because it was the only way I could figure out how to run a query where I didn't know which fields would be filled in. This is my first foray into massive SELECT statements and I just want to know if I'm setting up the SELECT correctly and what index methods I could use to speed up my SELECTS. Currently now a standard SELECT gets concatenated as such... SELECT recid,surname,firstname,yy,county,port_arr,sex,seek_first,seek_surname,seek_rel from main WHERE recid IS NOT NULL AND ((soundex(firstname) = soundex('$firstname')) OR (firstname LIKE '%$firstname%')) AND (age = '$age') This varies a great deal as to which fields are filled in. I just want to write the fastest selects i can. Any kind soul willing to offer a hint or a book I should be reading?
SELECT From Multiple Tables With Different Columns
I am trying to combine 2 SELECT queries so that I can order them to display in proper date order. The queries I am using are below. My question is, how can I join together these queries so that they will mix the news and events together to display them in order instead of displaying the news items first in date order then displaying the events beneath them in their order. $query = "SELECT * FROM news_articles WHERE (date_full >= '$days_back' AND date_full <= '$days_ahead') AND (news_type = '$news_query') ORDER BY 'date_full' DESC"; $query = "SELECT calendar.* FROM calendar LEFT JOIN events_type ON events_type.event_id = calendar.id WHERE calendar.date_full >= '$eventdate_time' AND date_full <= '$days_time_ahead' AND STATUS = 'active' AND (events_type.type_title = '$events_query') ORDER BY 'date_full' ASC"; Anyone got any ideas?
Merging Multiple SELECT Queries
I have two tables, one storing the info of the users ( one user in each record ) and the other the ids of several users in a single record i.e. (4,2,9,6), (4,2,8,3) etc. For convenience sake, i'll call it table1 and table2. What is need is to retrieve the records from table two and from the user ids, get all the users first name from table1. My problem is that i want to keep it within one query. I have tried looking up union and joins but they don't seem to meet my needs. What i would like to achieve is maybe (user1_first_name, user1, user2_first_name, user2 etc.) Is this possible at all?
Select Count From Multiple Tables
I want to select persons from a person table, and count from an "events", "trainings", and "leads" table, but I'm not able to figure out how to select distinct so that I get one person each and a count for how many times that person appears in each other table. My statement looks like this right now: SELECT person.name, count(leads.lead_id), count(events.event_id), count(trainings.training_id) FROM person LEFT JOIN leads ON leads.person_id=person.person_id LEFT JOIN eligible ON eligible.person_id=person.person_id LEFT JOIN events ON events.event_id=eligible.event_id LEFT JOIN trainings ON trainings.event_id=events.event_id GROUP BY leads.lead_id, events.event_id, trainings.training_id Is it even possible to do what I want?
Multiple Sums In A Single SELECT
I must have a single select execute 2 sums, from two different tables. For example: SELECT tbl1.ID as ID, SUM(tbl1.value1) AS Money_Earned, SUM(tbl2.value2) AS Money_Spent FROM income tbl1, expenses tbl2 WHERE tbl1.ID = tbl2.ID GROUP BY tbl1.ID I get a table back in the form I expect, but instead of a sum, it seems I'm getting a product of the number of entries in the table that meet the criteria * the sum of those values (value1 for example in the first sum statement). In other words, if my table income table has 2 entries for ID = 1, and each value has a 5 and a 6 respectively, the result I'll get back is not 11, but rather 22.
Select Multiple Entries From One Column?
How can I select multiple snippets of data from a column in a database? I want to do something like this: SELECT * FROM computers WHERE make=AMD, both, all2 AND system=solar, all, all2 In PHP of course! How can I do this? It's for a computer configurator script. I want to sort everything in the database by price and I had to run multiple queries and because of that, its not going in the order I wanted. Can I select that many entries of data in one query?
|