duanliexi1052 2017-12-23 19:48
浏览 24
已采纳

使用左连接的Mysql查询 - 如果两行相同则使用specialize索引

I have a table called users and another called grouppost in my mysql database. Both tables have the id row in common. So when I make an sql query left joining the two tables how can I decide which id row I want?

Example

$sql = "SELECT gp.*, u.*
      FROM grouppost
      AS gp LEFT JOIN users AS u ON u.username = gp.author
      WHERE gp.gname = ? AND gp.type = ? ORDER BY gp.pdate DESC $limit";
      $stmt = $conn->prepare($sql);
      $stmt->bind_param("ss",$g,$zero);
      $stmt->execute();
      $result_new = $stmt->get_result();
      if ($result_new->num_rows > 0){
         while ($row = $result_new->fetch_assoc()) {
            $grouppost_id = $row["id"]; // I want to get the id from table grouppost but I get the id from the users table
         }
      }
  • 写回答

1条回答 默认 最新

  • doudouba4520 2017-12-23 19:55
    关注

    If you want to target it separately in the result set, you can alias it in your select:

    SELECT gp.*, u.*, gp.id AS grouppost_id 
    FROM grouppost AS gp 
    LEFT JOIN users AS u ON u.username = gp.author
    WHERE gp.gname = ? 
      AND gp.type = ? 
    ORDER BY gp.pdate DESC
    $limit
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址