473,387 Members | 1,798 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,387 software developers and data experts.

Select twice from two different columns in the same row and the same table

2
I got some tables I want to join. I know very basicly how to join tables, but I'm no good at it and I don't have much experience with it...

I have two tables:

_________________________________
| meeting |
-----------------------------------------------------------
| meetingid | person1id | person2id | other |
-----------------------------------------------------------

______________
| people |
-------------------------
| peopleid | name |
-------------------------


I have the meetingid, and I want to select the meetingid, name of the person1id, name of the person2id and other.

If it makes any difference, it's gonna be used alongside php. :)

Thanks in advance!
Nov 29 '09 #1
2 2541
Atli
5,058 Expert 4TB
Hey.

You can JOIN the `people` table with the `meeting` table twice; once for each person you need to join.

For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     m.meeting_id, m.other,
  3.     p1.id AS 'p1_id', p1.name AS 'p1_name',
  4.     p2.id AS 'p2_id', p2.name AS 'p2_name'
  5. FROM meeting AS m
  6. LEFT JOIN people AS p1
  7.     ON m.person1_id = p1.people_id
  8. LEFT JOIN people AS p2
  9.     ON m.person2_id = p2.people_id
  10. WHERE 
  11.     m.meeting_id = 1; -- or something
P.S.
As a general rule, it is best to avoid using plural words for table names. Meaning; rather than name the table 'people', name it 'person'. That way you can avoid awkward names like 'peopleid' (Which makes little sense, really)
Nov 30 '09 #2
Udyret
2
Thanks, yeah, the names etc isn't the exact stuff that I actually use. Just something to make the example easy! But thanks to you, it now works and I've got more insight on how to write mysql queries with join.
Nov 30 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rob Knowles | last post by:
I have created a script that runs once a week and copies data from one table (phpbb_users is the actual table name) in a database called users I have a table called users. There are two tables...
4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
10
by: Robert Stearns | last post by:
In a table which has an animal id, its dam id and sire id, I want to see a combined list of all animal ids. I tried the following, which did not work, but did not give any error messages: select...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
8
by: carlospedr | last post by:
I have to insert data from about 30 tables into a single table (Users), to do so i used a cursor and a bit of dynamic sql, this should work fine if the tables have to do the select from had the...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
2
omerbutt
by: omerbutt | last post by:
hi there i have to select 7 columns from table1, 1 column from table2, and 1 column from tables3 and show the result but i am not getting it right the main thing that i am trying to achieve is that i...
1
by: canugi | last post by:
I need to store the contents of an SQL "in clause" in an MS Access 2000 table. I use MS Access version 9.0.8960 (SP3) This is my statement (and it works fine with the explicit "in clause"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.