473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jul 17 '05 #1
2 2041
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.
Jul 17 '05 #2
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

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Private Pyle | last post by:
AIX 5.1, DB2 8.1.3 64-bit ESE 5 partitions 1 catalog, 4 data. I have a situation where I have to update 269,000,000 rows in a table with the value in another table with just about the same number...
9
by: Neil Ginsberg | last post by:
I have a strange situation using Access to automate a Word mail merge. Using Access 2000 and Word 2000, the code opens Word, opens the document in Word, sets a table in the calling Access...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
0
by: Phil C. | last post by:
Hi, I'm using Access 2000. I have a Select Query that uses the MID function to separate the actual text of articles from the title of the articles. The articles are enterd into the...
8
by: Ron B | last post by:
Help!!! What am I doing wrong? I am working with Office 2003 and am trying to create a command button on an Access form that will create a mail merge in Word from an Access table. I want to...
7
by: Defected | last post by:
Thanks all for help, I have tried this code but it's don't work. -------------------------------------------------------------------- Structure typedef struct{ int atomic_number; char...
16
by: Sam Durai | last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes even 0 rows) to a big table (of rows around 4 billion). I used the PK of the big table as merge key but merge does a table scan...
6
by: amitabh.mehra | last post by:
I want to trap errors (RI errors), if any, that might turn up during merge in DB2. Is there some feature like the one in Oracle: MERGE INTO... WHEN MATCHED THEN UPDATE... WHEN NOT MATCHED THEN...
1
by: UDBDBA | last post by:
Hi, If someone can clarify this, it would be of great help! We have a merge, which is written *only* to update the target table. but the access plan shows branches with inserts and deletes on...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.