MySql question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
smoky989
Forum Commoner
Posts: 41
Joined: Mon Sep 02, 2002 1:14 pm

MySql question

Post by smoky989 »

OK, here's my situation...I have three tables. Each of them have a field called name, position, and email. l want to write a query thta will list the names from the 3 tables, the positions from the three tables, and the email from the three tables. I want to do this with 3 columns. It seems like something thta can be done but I'm drawing a blank. So...

column 1 - names from the three tables
column 2 - positions from the 3 tables
column 3 - email from the three tables

any ideas? Thanks in advance.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Am I right in thinking that you want something along the lines of:

Code: Select all

name1      |       position1        |        email1
name2      |       position2        |        email2
name3      |       position3        |        email3
name4      |       position4        |        email4
name5      |       position5        |        email5
etc...
Mac
smoky989
Forum Commoner
Posts: 41
Joined: Mon Sep 02, 2002 1:14 pm

Yes

Post by smoky989 »

Yeah thats exactly what I want. Three columns with the info from the three tables.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What code have you got so far?

Mac
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

Use temp tables to hold the data. This page from the MyQL manual explains how to select data from one or more tables and hold the results in a new temp table. This should do what your looking for.

http://www.mysql.com/doc/en/INSERT_SELECT.html

Keith
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

You could also use the UNION operator if you're using at least MySQL 4.0:

Code: Select all

select name, position, email
from table1
UNION
select name, position, email
from table2
UNION
select name, position, email
from table3
Post Reply