Jump to content

Select with 2 tables


stickman

Recommended Posts

I dont know if this is possible, but I was wondering if there was a way to only select a user from table A if that user has at least one row in table B. Here is how I do it in PHP:

[code]
$query = "SELECT * FROM `table_a` ORDER BY `user`";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
    $query2 = "SELECT * FROM `table_b` WHERE `user_id` = '{$row['id']}'";
    $result2 = mysql_query($query2);
    if(mysql_num_rows($result2)==0) {continue;}
    echo $row['user'];
    echo "<br>";
}
[/code]

That will output every user that has at least one row in table B. I was wondering if there was any way to do that with 1 SELECT statement?

Thanks in advance for any help!!!
Link to comment
https://forums.phpfreaks.com/topic/15421-select-with-2-tables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.