mysql Selecting from two different tables.

后端 未结 9 926
终归单人心
终归单人心 2021-01-28 05:01
$sql = \"select body, stamp from posts where user_id = \'$userid\' order by stamp desc\";

NOTE: the above query works fine. What I want to do is also s

9条回答
  •  梦谈多话
    2021-01-28 06:05

    You can use table name to select the columns. Ex:

    $Query = "select table1.body, table1.stamp, users.username from posts, users where user_id = '$userid' order by stamp desc";
    

    But, this method is not good in performance.

    The best method is:

    $Query = "SELECT table1.body, table1.stamp, users.username 
    FROM posts 
    INNER/LEFT/RIGHT JOIN users 
    ON users.user_id = '$userid' AND users.user_stamp = stamp.stamp_id
    

    All the tables must be related.

    Greetings,

提交回复
热议问题