I have 3 tables that are set up like so:
Quotes Table Stores Table
id store_id date_submitted store_number code
1 15 1/29/13 23:13 15 ANT
2 16 1/30/13 7:47 16 MAT
Leads Table
code user_name
MAT User1
ANT User2
What I want to be able to do is have the query return all the values in the quotes table, and then the code values from the Stores table, and the user_name from the leads table. I would like it to look something like this:
id store_id date_submitted code user_name
1 15 1/29/13 23:13 ANT User2
2 16 1/30/13 7:47 MAT User1
I have the following query that I wrote:
SELECT id, store_id, date_submitted, code, user_name FROM quotes q, stores s, leads l WHERE CONCAT('%', q.store_id, '%') LIKE CONCAT('%', s.store_number, '%') AND s.code=l.code AND date_submitted > "2013-01-01 00:00:00" AND date_submitted < "2013-01-31 23:59:59" GROUP BY q.id
The problem I’m running into, is the database isn’t matching the code to the correct store_id.
I’m not sure what I’m doing wrong here, I have looked at a couple of SO posts, and googled for it to no avail.
Any assistance would be appreciated.
Well, why are you using wildcards (
%) to joinstore_idwithstore_number?, can’t you use an exact match?. Also, just for clarity, you should use explicitJOINs: