Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.