merge select from 2 table

This is a discussion on merge select from 2 table within the PHP Language forums, part of the PHP Programming Forums category; I've 2 tables where I want to retrieve the id and name from and merge them: table1: IDtable1-nametable1 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-25-2005
Bob Bedford
 
Posts: n/a
Default merge select from 2 table

I've 2 tables where I want to retrieve the id and name from and merge them:

table1:
IDtable1-nametable1
1 - 578445
2 - 454545
3 - 554887

table2:
IDtable2-nametable2
X1 - 544512
X2 - 445775

The result should look like (order by nametable)
X2 - 445775
2 - 454545
X1 - 544512
3 - 554887
1 - 578445

(this result will be used for filling a combobox).
I may excecute 2 queries, put the results in an array and then order it, but
I'm sure there is a better way in MySQL, but I couldn't find it.

Bob


Reply With Quote
  #2 (permalink)  
Old 01-25-2005
Colin McKinnon
 
Posts: n/a
Default Re: merge select from 2 table

Bob Bedford wrote:

> I've 2 tables where I want to retrieve the id and name from and merge
> them:
>

<snip>

Depends on the version of MySQL. More recent ones will allow:

( SELECT id, value, 'a' as tableid from table1 )
UNION
( SELECT id, value, 'b' as tableid from table2 )
ORDER BY tableid

HTH

C.
Reply With Quote
  #3 (permalink)  
Old 01-25-2005
Steve
 
Posts: n/a
Default Re: merge select from 2 table


> I've 2 tables where I want to retrieve the id and name from and merge

them:

Alternatively, use a temporary table to simulate a UNION (if your
version of MySQL doesn't support it)...


CREATE TEMPORARY TABLE MyTempTable ( ID INT, Name VARCHAR(25) )

INSERT INTO MyTempTable ( ID, Name )
SELECT IDtable1, nametable1
FROM Table1

INSERT INTO MyTempTable ( ID, Name )
SELECT IDtable2, nametable2
FROM Table2

SELECT ID, Name
FROM MyTempTable
ORDER BY Name

---
Steve

Reply With Quote
Reply
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

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




All times are GMT +1. The time now is 10:13 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0