【问题标题】:MySql select with Limit 1 when using a join with 2 tables使用 2 个表的连接时,MySql 选择限制为 1
【发布时间】:2014-09-10 21:26:28
【问题描述】:

我实际上是在尝试使用限制从amgb 中选择一条记录,同时与amga 进行连接。

我的桌子是这样的:

amga

"id"    "itemId"    "itemTempId"    "itemName"                  "itemCountry"   "userId"
"1"     "US1"       "T001"          "Samsung Galaxy Note 5"     "US"            "1"
"2"     "CA2"       "T002"          "Samsung Galaxy Note 6"     "CA"            "2"
"3"     "UK3"       "T003"          "Samsung Galaxy Note 7"     "UK"            "3"

amgb

"id"    "itemId"    "itemTempId"    "itemImageName"     "userId"
"1"     "US1"       "T001"          "front.jpg"         "1"
"2"     "US1"       "T001"          "side-left.jpg"     "1"
"3"     "US1"       "T001"          "side-right.jpg"    "1"
"4"     "US1"       "T001"          "back.jpg"          "1"
"5"     "CA2"       "T002"          "front.jpg"         "2"
"6"     "CA2"       "T002"          "side-left.jpg"     "2"
"7"     "CA2"       "T002"          "side-right.jpg"    "2"
"8"     "CA2"       "T002"          "back.jpg"          "2"
"9"     "UK3"       "T003"          "front.jpg"         "3"

用简单的语言来说,这相当于: select itemName from amga where itemId = 'US1' and userId = 1 along with 1 itemImageName from amgb where itemId = sameOneAsBefore and userId = sameOneAsBefore

我希望的最终结果就像这样简单: Samsung Galaxy Note 5 | front.jpg - 这是来自 amga 的 itemName 和来自 amgb 的 itemImageName 其中 userId = 1 和 itemId = US1

但是,尽管所有连接和其他一切都正确,但我看不出哪里出错了。什么可能导致问题/* SQL Error (1054): Unknown column 'b.userId' in 'on clause' */

SELECT  a.itemName, b.itemImageName 
FROM amga a 
LEFT JOIN (
    SELECT
        itemImageName
    FROM
        amgb
    LIMIT 1
) b
ON a.userId = b.userId and a.itemId = b.itemId  
WHERE a.userId = 1 and a.itemId = 'US1';

【问题讨论】:

  • 您的Left Join 中没有名为UserId(或itemId)的列。您拥有的唯一列是itemImageName。错误直接告诉你。
  • 我有它。为什么要在那里再次选择它?
  • 您不能Join On 一个不存在的列。如果您没有在子选择中选择它,则它不存在。您需要将UserIdItemId 添加到Left Join Select 语句中。

标签: mysql sql


【解决方案1】:

如果子查询返回一列且不超过一条记录,您可以将其作为列包含在查询中:

SELECT  a.itemName,
  (SELECT itemImageName
    FROM amgb b
    WHERE a.userId = b.userId AND a.itemId = b.itemId
    LIMIT 1
  ) AS itemImageName
FROM amga a 
WHERE a.userId = 1 AND a.itemId = 'US1';

【讨论】:

  • If subquery returns one column and no more than one record you can include it as column in query - 是的,它只会返回一列。所以你做的没错吧?
  • 是的,因为它只返回一个值或 null(如果没有记录),更多信息:dev.mysql.com/doc/refman/5.6/en/scalar-subqueries.html
【解决方案2】:

尝试使用这个:-

SELECT  a.itemName, b.itemImageName 
FROM amga a 
LEFT JOIN (
           SELECT itemId, userId, itemImageName
           FROM amgb
           LIMIT 1
          ) b
ON a.userId = b.userId and a.itemId = b.itemId  
WHERE a.userId = 1 and a.itemId = 'US1';

【讨论】:

    【解决方案3】:
    SELECT  a.itemName, b.itemImageName 
    FROM amga a 
    LEFT JOIN (
        SELECT
            itemImageName, userId
        FROM
            amgb
        LIMIT 1
    ) b
    ON a.userId = b.userId and a.itemId = b.itemId  
    WHERE a.userId = 1 and a.itemId = 'US1';
    

    【讨论】:

      【解决方案4】:

      你可以试试这个

      SELECT  a.itemName, b.itemImageName 
      FROM amga a 
      LEFT JOIN (
          SELECT
              itemImageName
          FROM
              amgb b
          LIMIT 1
      ) <= 1
      ON a.userId = b.userId and a.itemId = b.itemId  
      WHERE a.userId = 1 and a.itemId = 'US1';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 2023-01-15
        • 2023-03-26
        • 2018-10-10
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多