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 07-19-2009, 06:25 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default Mysql Query to return 2 columns as 1

I am looking to return 2 columns from 2 different tables as 1 column - is this possible?

eh.

Table1
ID DATA1
10 blahblah
15 blahblah

Table2
ID DATA2
20 blahblah

Say I want to write a query that will return 10,15,20 how would I do this assuming ID is declared the same on both tables?
captainmerton is offline  
Reply With Quote
Old 07-19-2009, 06:30 PM   #2 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Please ignore - I just discovered the UNION command.
captainmerton is offline  
Reply With Quote
Old 07-20-2009, 04:35 AM   #3 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

UNION is very slow as it uses temporary tables. Using a JOIN would be faster.
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
Old 07-20-2009, 10:55 AM   #4 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Any examples of how I can achieve the 1 merged column using a JOIN? i've used a UNION but am concious of performance so would prefer to use a JOIN.
captainmerton is offline  
Reply With Quote
Old 07-20-2009, 12:12 PM   #5 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

Example
SELECT Data1,Data2
FROM Table1
JOIN Table2 ON Table1.id = Table2.id

I have just noticed you do not have the same ID in both tables???
QUOTE "declared (ID) the same on both tables"

Are the IDs in both tables foreign keys?
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
Old 07-20-2009, 12:40 PM   #6 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

What you have written will simply return rows in the format:

data1,data2
data1,data2

Using my example say the column names on either table are different i.e.

Table1
ID1 DATA1
10 blahblah
15 blahblah

Table2
ID2 DATA2
20 blahblah

I want a way of returning the data like this:

ID1
ID1
ID2
ID1

as opposed to this:

ID1,ID2
ID1,ID2

Both ID columns are declared the same eg CHAR(2).

I can get what i want using a UNION but cant find a join to do it. I really dont think your basic join example will provide what i'm looking for needs further honed.
captainmerton is offline  
Reply With Quote
Old 07-20-2009, 12:54 PM   #7 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

Sorry I miss understood what you were trying to achieve.
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios is offline  
Reply With Quote
Old 07-20-2009, 01:13 PM   #8 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Still - any idea whether what i want to achieve can be done with a JOIN? I'm no database expert but just looking at the UNION I have doing the job just now I suspect it isnt the most efficient of solutions.
captainmerton is offline  
Reply With Quote
Old 07-20-2009, 01:53 PM   #9 (permalink)
The Acquainted
 
JaoudeStudios's Avatar
 
Join Date: Jul 2009
Location: Surrey
Posts: 105
Thanks: 1
JaoudeStudios is on a distinguished road
Default

As far as I know you would need a UNION.
__________________
JaoudeStudios.com | JaoudeStudios.com Forum | JaoudeStudios.com Blog
OpenSource is the road ahead...!
JaoudeStudios 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
Stream problem Tanax Advanced PHP Programming 9 06-10-2009 12:33 PM
MySQL query help sjaq Advanced PHP Programming 4 05-04-2009 10:33 AM
Next class project? allworknoplay The Lounge 6 04-18-2009 08:33 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Problem with mysql_real_escape_string() ETbyrne Advanced PHP Programming 0 12-17-2008 10:39 PM


All times are GMT. The time now is 02:28 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