【问题标题】:Selecting only one row for variable that isn't unique只为不唯一的变量选择一行
【发布时间】:2022-11-04 00:01:45
【问题描述】:

我只想选择 Id 为“productId”的一行,我现在显示所有三行的“productId”为 3,如何只显示 1 行?

      $sql = "SELECT * from Product
      inner join OrderItems ON Product.productId = OrderItems.productId";

任何帮助将不胜感激

【问题讨论】:

  • 你如何决定你想要 3 行中的哪一行?这是解决这个问题的关键。
  • 如果您想随机选择其中一个,请将“LIMIT 1”放在 SQL 代码的末尾。

标签: mysql


【解决方案1】:
SELECT * from Product
      inner join OrderItems ON Product.productId = OrderItems.productId order by Product.id limit 0,1;

您可以使用与上述类似的东西。根据您要使用的排序更改 order by 子句。

【讨论】:

    【解决方案2】:

    在乔的推动下,这行得通!

    $sql = "SELECT * from Product
    inner join OrderItems ON Product.productId = OrderItems.productId where Product.size = OrderItems.size and Product.color = OrderItems.colors";
    

    【讨论】:

    • 很高兴轻推。 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多