One variable row inside another variable row

前端 未结 3 1878
庸人自扰
庸人自扰 2021-01-28 16:09

I have following script:

$sql = \"SELECT * FROM `users`\"
$q = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($q);
$sql1 = \"SELECT * FROM `ot         


        
3条回答
  •  萌比男神i
    2021-01-28 16:53

    SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username
    

    I'm assuming you want to do this because you want to be able to access all the rows from either table where a particular user name is the same (e.g. the data from users where username="john" and the data from other_table where username="john" for all usernames). No need to nest a result set to do this, just use a JOIN statement and then you can access all the columns as if it was a single result set (because it is):

    $sql = "SELECT * FROM users AS u INNER JOIN other_table AS o ON u.username = o.username";
    $q = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($q);
    
    $item = $row['any_column'];
    

    FYI you should list out the column you want to retrieve instead of using *, even if you want to retrieve them all, as it is better practice in case you add new columns in the future.

提交回复
热议问题