473,403 Members | 2,183 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,403 software developers and data experts.

MySQL Query on multiple tables

3
I have 2 tables linked by a common field.
Expand|Select|Wrap|Line Numbers
  1. users
  2. ------
  3. id
  4. ....more columns
  5.  
  6. users_profiles
  7. --------------
  8. users_id
  9. ...more columns
  10.  
The user id in table users matches the users_id in table users_profile.

I need to querry from users, get it's match in users_profile and display the reults in php page.


This is what I got so far, but the result page is blank.
Expand|Select|Wrap|Line Numbers
  1. // Make a MySQL Connection
  2. // Construct our join query
  3. $query = "SELECT users.id, users_profile.users_id FROM users, users_profile WHERE users.id = users_profile.id";
  4.     // SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1; 
  5. $result = mysql_query($query) or die(mysql_error());
  6.  
  7.  
  8. // Print out the contents of each row into a table 
  9. while($row = mysql_fetch_array($result)){
  10.     echo $row['id']. " - ". $row['users_id'];
  11.     echo "<br />";
  12. }
  13.  
Thanks for the help, I am a novice at this and it's driving me crazy!
Jan 19 '08 #1
4 3164
Atli
5,058 Expert 4TB
Hi. Welcome to TSDN!

In your SQL query you used multiple tables in your FROM clause. That's the most basic and simple version of a table JOIN you can do.

I'd recommend a slightly more complex version of a JOIN, which would clarify the query (imo anyways :P) and could possibly reduce the load this query would put on the server.

Joining tables the way you do in your query, the MySQL server simply compiles a table of every possible combination of every column in the table, which can get extremely large!, and then uses your WHERE clause to filter out all the rows that do not have matching user id's.

If you were to use a INNER JOIN, you could specify how MySQL compiles the table by giving it a list columns on which to combine. This results in a far smaller table and is in many ways easier to use.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. SELECT users.id, users_profiles.users_id FROM users
  2. INNER JOIN users_profiles
  3.   ON users.id = users_profiles.users_id
  4.  
Jan 19 '08 #2
cjacks
3
Here is the result of your code. Getting closer.
2 - 2 -
20 - 20 -
37 - 37 -
41 - 41 -
53 - 53 -
56 - 56 -
57 - 57 -
60 - 60 -
69 - 69 -
71 - 71 -
73 - 73 -


Something wrong in my results statement:

[PHP] $result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
echo $row['id']. " - ". $row['users_id']. " - $real_name ";
echo "$real_name";
echo "<br />";[/PHP]
Jan 19 '08 #3
cjacks
3
Hi. Welcome to TSDN!

In your SQL query you used multiple tables in your FROM clause. That's the most basic and simple version of a table JOIN you can do.

I'd recommend a slightly more complex version of a JOIN, which would clarify the query (imo anyways :P) and could possibly reduce the load this query would put on the server.

Joining tables the way you do in your query, the MySQL server simply compiles a table of every possible combination of every column in the table, which can get extremely large!, and then uses your WHERE clause to filter out all the rows that do not have matching user id's.

If you were to use a INNER JOIN, you could specify how MySQL compiles the table by giving it a list columns on which to combine. This results in a far smaller table and is in many ways easier to use.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. SELECT users.id, users_profiles.users_id FROM users
  2. INNER JOIN users_profiles
  3.   ON users.id = users_profiles.users_id
  4.  
I figured it out. Had to place all the fields I wanted to view in the query statement. Thanks for the help!!!
Jan 20 '08 #4
Atli
5,058 Expert 4TB
Glad you got it working! :)
Jan 21 '08 #5

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

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
6
by: jacob nikom | last post by:
I would like to create data model for a group of stores. All stores in this group are very similar to each other, so it is natural to allocate one MySQL database per store. Each database is going...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
33
by: Joshua D. Drake | last post by:
Hello, I think the below just about says it all: http://www.commandprompt.com/images/mammoth_versus_dolphin_500.jpg Sincerely, Joshua Drake
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
3
by: cdelarte | last post by:
I would like to be able to mail merge records from multiple mysql tables using a simple template, preferably via a command line script. MSWord mail merge via ODBC will not work for me as it only...
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.