Jump to content

Sql Select statement.

awyatt7159's Photo
Posted Mar 25 2013 01:02 PM
203 Views

Hi, Can you please help me with my code?

What I want to do is display information from two or more tables. The information is uid and created form the users table, and field_gender_value from the field_data_field_gender table. The 2009 book learning SQL shows how to display information from one table, not 2.

My select statement show the information I need to display. However, each user id, gender, and created date are displayed seven times instead of once. What am I doing wrong?

<?php // query.php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());

$query = "SELECT *FROM users,field_data_field_gender";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());

$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{

echo 'User ID: ' . mysql_result($result,$j,'uid') . '<br />';
echo 'Gender: ' . mysql_result($result,$j,'field_gender_value') . '<br />';
echo 'created: ' . mysql_result($result,$j,'created') . '<br />'. '<br />';
}
?>

Thank you

Tags:
0 Subscribe


1 Reply

0
  mhalverson's Photo
Posted Mar 25 2013 01:45 PM

Chapter 5 and chapter 10 of "Learning SQL" will cover the information that you need.

I cannot fully answer your question without knowing more about what your tables look like - what are the columns in each and how do they relate. I'll try to answer as best as I can without that information.

When you are trying to query two tables, your are performing what is called a "Join". There are several different types of these, and which one you want will depend on what you are trying to do.

The way that you have written your query, it is performing a "Cross Join" or catesian product. This means that you will get back all combinations of rows. Thus if your first table has 3 rows and your second has 2, you will return 6 rows in total. Assume that table 1 lists several math classes and table 2 lists ta's along with the class they are assigned to (by reg_no).

Table_1 (columns are reg_no and title)
---------------------------------------------------
Row 1: m123 Calculus
Row 2: m321 Algebra
Row 3: m211 Geometry

Table_2 (columns are ta_name and class)
------------------------------------------------------
Row 1: James m123
Row 2: Sara m321

Your query would be "SELECT * FROM Table_1, Table_2". This will return all combinations.

Result (columns are reg_no, title, ta_name, class)
------------------------------------------------------------------
m123 Calculus James m123
m321 Algebra James m123
m211 Geometry James m123
m123 Calculus Sara m321
m321 Algebra Sara m321
m211 Geometry Sara m321

I expect this is what you are seeing.

This is likely not what is wanted in this situation. Now suppose that I wanted to get a list of all classes along with the assigned TA. I would use an "INNER JOIN". This is used when two columns relate - in this case the class column in Table_2 matches the reg_no column in Table_1.

My query is "SELECT * FROM Table_1 INNER JOIN Table_2 ON Table_1.reg_no=Table_2.class". The ON clause tells how the information relates. This will return only two columns.

Result
--------
m123 Calculus James m123
m321 Algebra Sara m321

Now, I only get the combinations that I am interested in. Alternatively, I could ask for the cross product and filter the results with a WHERE clause. "SELECT * FROM Table_1, Table_2 WHERE Table_1.reg_no=Table_2.class". This will return exactly the same results.

If you have Learning SQL (which I assume you do as you mentioned it) please read chapters 5 and 10 closely. Joins can be tricky, but they are the real power in the relational data model.