TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-24-2010, 07:46 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default JOIN query

I'm trying join 2 tables and think I require a LEFT join as all rows in table1 are required but struggling to handle the rules associated with table2. Looking for some help/guidance. My tables are basically:

TABLE1
userid. user
1. Mr Smith
2. Mr Davis
3. Mrs Jackson

TABLE2
imageid. Main_image. userid
23. Y 1
24. N. 1
25. Y. 2

I'm looking for a query that will return every row in table1 then rows associated in table2 assuming the main_image flag is set to Y.

I've done a left join but when append AND main_image = Y i lose rows from table1 that don't match that criteria I.e. User 3. How can I pull all users from table1 then pull in all main_images from table2?

Hope u get the gist having trouble easily documenting this as typing it on an iPhone but any help greatly appreciated.
captainmerton is offline  
Reply With Quote
Old 11-24-2010, 10:50 PM   #2 (permalink)
The Wanderer
 
Schroder's Avatar
 
Join Date: Mar 2008
Posts: 20
Thanks: 0
Schroder is on a distinguished road
Default

I think you need to add an "OR imageid IS NULL" so if there's no associated record in the second table, the user record will still be returned.
Schroder is offline  
Reply With Quote
Old 11-25-2010, 07:09 AM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

something like

(The associated record was userid)

Code:
select * from table1 left join table2 on table1.userid = table2.userid


+--------+-----------+-------+--------+--------+
| userid | name      | imgid | img    | userid |
+--------+-----------+-------+--------+--------+
|      1 | Mr Smith  |     3 | image3 |      1 |
|      2 | Mr Jones  |     1 | image1 |      2 |
|      2 | Mr Jones  |     2 | image2 |      2 |
|      3 | Mrs Jones |  NULL | NULL   |   NULL |
|      4 | Mr Patel  |     4 | image4 |      4 |
|      4 | Mr Patel  |     5 | image5 |      4 |
+--------+-----------+-------+--------+--------+
Another option is :

Code:
 select * from table1, table2 where table1.userid = table2.userid;
+--------+----------+-------+--------+--------+
| userid | name     | imgid | img    | userid |
+--------+----------+-------+--------+--------+
|      1 | Mr Smith |     3 | image3 |      1 |
|      2 | Mr Jones |     1 | image1 |      2 |
|      2 | Mr Jones |     2 | image2 |      2 |
|      4 | Mr Patel |     4 | image4 |      4 |
|      4 | Mr Patel |     5 | image5 |      4 |
+--------+----------+-------+--------+--------+
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 11-26-2010, 12:42 PM   #4 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Schroder thanks your solution solved it. In my head i knew what i wanted to do but had the following:

OR imageid = NULL

which didnt bring back the rows where the image columns were null. Changed that to:

OR imageid IS NULL

and it worked.

Cheers.
captainmerton is offline  
Reply With Quote
Old 12-31-2010, 05:38 PM   #5 (permalink)
The Contributor
 
Join Date: Apr 2005
Location: Kent, UK
Posts: 54
Thanks: 0
Dr John is on a distinguished road
Default

this bit OR imageid = NULL
fails because no value can be equal to null. (it's an sql rule)
The IS NULL gets round this problem.

you can also use ifnull( table1.imageid, 0 ) AS imageID to substitute 0 (or even 'unknown') for null, which looks better to users who don't understand null.
__________________
www.kidneydialysis.org.uk
Dr John is offline  
Reply With Quote
Old 12-06-2012, 11:29 AM   #6 (permalink)
The Wanderer
 
Join Date: Nov 2012
Posts: 15
Thanks: 0
abbot is on a distinguished road
Default

good practice.
abbot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is good MySQL practice? Aaron MySQL & Databases 7 11-23-2012 10:56 AM
Query With a C# Variable?‏ StevenF The Lounge 5 12-08-2008 05:32 AM
UNION query doesn't work...? Dave MySQL & Databases 0 09-20-2008 07:22 PM
Tables joining other Tables? Orc MySQL & Databases 6 02-09-2008 09:42 PM
Mysql problem, most likely related to left join Village Idiot MySQL & Databases 6 01-04-2008 06:09 AM


All times are GMT. The time now is 09:38 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design