Jump to content

Select Multiple Tables Without A Join


tomtimms

Recommended Posts

I need to select columns from 2 tables however not user a join.  It doesn't seem to want to work, is my second select correct where I have the 2 columns equaling each other?

 

SELECT a.date AS date, a.source AS source, SUM(a.total) AS total, (SELECT SUM(b.search) FROM table2 b WHERE a.date = b.date AND a.source = b.source) AS search FROM table1 a WHERE a.date BETWEEN '2010-08-01' AND '2010-08-31' GROUP BY date, source

Link to comment
https://forums.phpfreaks.com/topic/212289-select-multiple-tables-without-a-join/
Share on other sites

Hi

 

Why do you not want to use a JOIN?

 

I think you probably need a GROUP BY clause on the subselect.

 

Best I can make out that you want (but using a join) is as follows

 

SELECT a.date AS date, a.source AS source, SUM(a.total) AS total, 
FROM table1 a
INNER JOIN (SELECT date, source, SUM(b.search) AS search FROM table2 b GROUP BY date, source) b
ON a.date = b.date AND a.source = b.source
WHERE a.date BETWEEN '2010-08-01' AND '2010-08-31' 

 

All the best

 

Keith

I need to select columns from 2 tables however not user a join.  It doesn't seem to want to work, is my second select correct where I have the 2 columns equaling each other?

 

SELECT a.date AS date, a.source AS source, SUM(a.total) AS total, (SELECT SUM(b.search) FROM table2 b WHERE a.date = b.date AND a.source = b.source) AS search FROM table1 a WHERE a.date BETWEEN '2010-08-01' AND '2010-08-31' GROUP BY date, source

 

what part is not working?.... works for me....  what datatype is your column 'date'  is a DATE or DATETIME?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.