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

join tables?

hi. i have two tables.

table one ("users") contains two columns, "user_id", and "user_name"
table two ("spam") contains three columns "msg_id","user_id", and "msg"

basically, i want to retrieve all the "spam" entries, but instead of
displaying the user_id, i want it to show the user_name, from table
one. (using php)

i'm assuming i need to use this JOIN thing, but i've never used it
before. any help?

Aug 20 '06 #1
4 4335

Mark wrote:
hi. i have two tables.

table one ("users") contains two columns, "user_id", and "user_name"
table two ("spam") contains three columns "msg_id","user_id", and "msg"

basically, i want to retrieve all the "spam" entries, but instead of
displaying the user_id, i want it to show the user_name, from table
one. (using php)

i'm assuming i need to use this JOIN thing, but i've never used it
before. any help?
err..by sort of guessing, i came up with "SELECT * FROM spam LEFT JOIN
users USING (user_id)" which seems to work alright. is that how you're
supposed to do it?

Aug 20 '06 #2
Mark wrote:
hi. i have two tables.

table one ("users") contains two columns, "user_id", and "user_name"
table two ("spam") contains three columns "msg_id","user_id", and "msg"

basically, i want to retrieve all the "spam" entries, but instead of
displaying the user_id, i want it to show the user_name, from table
one. (using php)

i'm assuming i need to use this JOIN thing, but i've never used it
before. any help?
SELECT users.user_name,spam.msg_id ,spam.msg FROM users.spam WHERE
spam.user_id = users..user_id

Aug 20 '06 #3
Mark wrote:
err..by sort of guessing, i came up with "SELECT * FROM spam LEFT JOIN
users USING (user_id)" which seems to work alright.
It might work a bit faster as following:

SELECT * FROM spam JOIN users USING (user_id);

You would use LEFT JOIN if you thought that there might be no entries in
the users table matching the user_id given in the spam table. I would
expect in your case that there should be a user in all such cases, so a
simple inner join would be fine. Inner joins often run quicker than
outer joins.

Regards,
Bill K.
Aug 20 '06 #4

Bill Karwin wrote:
Mark wrote:
err..by sort of guessing, i came up with "SELECT * FROM spam LEFT JOIN
users USING (user_id)" which seems to work alright.

It might work a bit faster as following:

SELECT * FROM spam JOIN users USING (user_id);

You would use LEFT JOIN if you thought that there might be no entries in
the users table matching the user_id given in the spam table. I would
expect in your case that there should be a user in all such cases, so a
simple inner join would be fine. Inner joins often run quicker than
outer joins.

Regards,
Bill K.
ohhh.. i see. thank you! this helps.

Aug 21 '06 #5

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

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
1
by: TeleTech1212 | last post by:
I am trying to select specific columns from multiple tables based on a common identifier found in each table. For example, the three tables: PUBACC_AC PUBACC_AM PUBACC_AN each have a...
8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
4
by: Anthony Robinson | last post by:
I was actually just wondering if someone could possibly take a look and tell me what I may be doing wrong in this query? I keep getting ambiguous column errors and have no idea why...? Thanks in...
2
by: dskillingstad | last post by:
I would really appreciate someone's help on this, or at least point me in the right direction.... I'm working on a permit database that contains 12 tables, and rather than list all of the...
2
by: Bennett Haselton | last post by:
I know how to create a DataAdapter that loads data from a data source into a table in a typed DataSet, and how to set the DataSource and DataMember properties of a DataGrid so that at run time it...
7
by: Shanimal | last post by:
I would like to know how to join 2 queries so that the results of these 2 queries show up in the same query: SELECT b.bios_serial_number FROM bios b: SELECT s.system_name FROM system s; ...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
3
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.