DotDragnet
May 23, 2012, 04:47:58 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Merging 2 SQLs  (Read 528 times)
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« on: April 11, 2010, 11:28:52 AM »

Hi all, more SQL help needed please  crybaby

I need to merge this:

select * from TABLE1 where c_code = $this->c_code

Note;  * pulls c_id, c_code, c_amount, c_type, c_status, c_use,  c_expiry_date

and this SQL:

select count(c_id) as c_used from TABLE2 where c_id = x

Where x is the c_id from table1.

Hope this makes sense?  Thank You in advance.

Logged
JasonD
Global Moderator
Hero Member
*****
Posts: 550



View Profile Awards
« Reply #1 on: April 11, 2010, 01:09:05 PM »

SELECT t1.*, COUNT(t2.c_id) AS c_used
FROM table1 t1 INNER JOIN table2 t2 USING (c_id)
WHERE t1.c_code = $this->c_code
Logged
Charisma Bypass
Hero Member
*****
Posts: 556



View Profile Awards
« Reply #2 on: April 11, 2010, 02:03:52 PM »

Hi Jason - many thanks  big grin

I had to change the inner join to a left join, and add a group by clause at the end.  Would this have any bearing on anything?  Checked teh codebase and it appears to work OK.

Thank you for setting me on the right track - highly appreciated!
Logged
JasonD
Global Moderator
Hero Member
*****
Posts: 550



View Profile Awards
« Reply #3 on: April 11, 2010, 03:11:34 PM »

Yeah, left join if there might not be any matching rows in table2. Group by if you are expecting more than one row.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!