Jump to content

Selecting only if there is a MySQL entry


bululu

Recommended Posts

Hi PHP Experts, I need some help.

I have two tables as follows

 

names table

id      name       man_no
1       Alex       12340
2       Anne       12341
3       Ben        12342
4       Jude       12343
5       Carlos     12344
6       Goofy      12345

scores table

id      score       man_no
1        12         12340
2        5          12341
3        0          12342

1st query

$query = "SELECT * FROM names'";
$result = mysqli_query($dbc, $query);
($row = mysqli_fetch_array($result));
$name = $row["name"];
$mat_no = $row["man_no"];

2nd query

$query = "SELECT * FROM scores WHERE man_no='$man_no'";
$result = mysqli_query($dbc, $query);
($row = mysqli_fetch_array($result));
$man_no = $row["man_no"];

Then

var_dump($score);

gives me

string '12' (length=2)
string '5' (length=1)
string '0' (length=1)
null
null
null

Now, here is my question:

 

The Names table will always have people who do not have scores as the scores are added when available. The first query gets the man numbers and using the man numbers, queries for each man number's associated score, returning nill where a score is unavailable. What I want is to get a result set that only incldes scores available in mysql without the null values, a way to avoid null values in the result set. I am still learning joints so I do ask that your help deals with individual queries as I am not yet at a I can use joins. Is it possible to restrict mysql results to only when an entry is available in MySQL.

 

In the above example, is there a clause I can add to the query to only return results where a value was found?

 

Or may be killing the null values after the query? I could not find a way to kill them!

 

My aim is to have:

string '12' (length=2)
string '5' (length=1)
string '0' (length=1)
Link to comment
Share on other sites

This is exactly what joins are for. Running a query on a result of another query is not very efficient.

 

The following query does what you are trying to do

SELECT n.id, n.name, n.man_no,                     # select these fields from names table
       s.score                                     # select these fields from score table
FROM names n                                       # alias names table to the letter n
LEFT JOIN scores s                                 # join scores table and alias to letter s
     ON n.man_no = s.man_no                        # get the scores where the man_no matches
WHERE s.score >= 0                                 # filter results where score is greater than zero


# Result
+----+------+--------+-------+
| id | name | man_no | score |
+----+------+--------+-------+
| 1  | Alex | 12340  | 12    |
+----+------+--------+-------+
| 2  | Anne | 12341  | 5     |
+----+------+--------+-------+
| 3  | Ben  | 12342  | 0     |
+----+------+--------+-------+

But to answer your question all you need to do is check to see if the second query returned result

$query = "SELECT * FROM names'";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result))
{
    $name = $row["name"];
    $man_no = $row["man_no"];

    $query2 = "SELECT score FROM scores WHERE man_no='$man_no'";
    $result2 = mysqli_query($dbc, $query2);
    while($row2 = mysqli_fetch_array($result))
    {
        if(mysqli_num_rows($result2) > 0)
        {
            $score = $row2['score'];
            echo "$name - $man_no - $score<br />";
        }
    }
}
Edited by Ch0cu3r
Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.