I asked a question earlier about extracting data from two table and got a beautiful answer which worked perfectly by I asked a question earlier about extracting data from two table and got a beautiful answer which worked perfectly by Ollie Jones
Here is the new situation
I want to get a certain data from the first table by adding a WHERE but now it fails and it seems as if it should not
This query does just fine when performing this function
table 1 table 2
product 1 product 4
product 2 product 2
product 3
product 4
SELECT ifnull( b.number >0, 0 ) purchased
FROM 1androidProducts a
LEFT JOIN (
SELECT count( * ) number, product
FROM 1androidSales
WHERE udid = ''
GROUP BY product
) b
ON a.appTitle = b.product
ORDER BY a.appTitle
LIMIT 0 , 30
yields this result
result
0
1
0
1
in short
for every item in table 1 shows this
a 0 if the table-1-item does not appear in table two
–OR–
a 1 if table-1-item does show in table 2
Now when I add this line
SELECT ifnull( b.number >0, 0 ) purchased
FROM 1androidProducts a
WHERE a.accountID = 2 <<---------- I added this line
LEFT JOIN (
SELECT count( * ) number, product
FROM 1androidSales
WHERE udid = ''
AND accountID = 2 <<---------- I added this line
GROUP BY product
) b
ON a.appTitle = b.product
ORDER BY a.appTitle
LIMIT 0 , 30
It now gives an error
1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘LEFT JOIN ( SELECT count( * ) number, product FROM 1androidSales ‘ at line 4
I simply want to get a subset of table 1 based on the accountID to compare against the second table.
so if I had this
table 1 table 2
accountid =1 product 1 accountid =2 product 4
accountid =1 product 2 accountid =2 product 2
accountid =1 product 3 accountid =2 product 5
accountid =1 product 4
accountid =2 product 1
accountid =2 product 2
accountid =2 product 3
accountid =2 product 4
accountid =2 product 5
would yield this result
result
0
1
0
1
1
so the total number result records matches the amount of records in table one that meet the accountID value of 2
I thought this would be an easy change but it is messing me up big time
Thanks for your time
I believe you have your first where clause in the wrong spot, put it after the join: