我的mysql查询问题

时间:2012-11-22 08:02:44

标签: mysql

嘿伙计们,我有这个疑问:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ = (SELECT Typ FROM lm_Artikel WHERE `Index` = (SELECT DISTINCT `Index` FROM lm_Warenkorb)))

但是当我运行它时,我收到了这个错误:

  

1242 - 子查询返回超过1行

我不知道我的查询中有什么错误。

lm_Schwellwert看起来的示例:

Typ     Schwellwert
Computer    78

lm_Artikel看起来的示例:

Index   Artikelbezeichnung  Status  Bestand     Lieferant    Typ
1           HP Elitebook        OK      19         HP         Computer     

lm_Warenkorb看起来的示例:

Index
11
1

5 个答案:

答案 0 :(得分:2)

条件只能在一个谷中运行(所以x = y等等) 在您的情况下,您的子查询

(SELECT Typ FROM lm_Artikel WHERE {索引{1}} {索引{1}}

返回多行,这会导致错误。

尝试修改您的查询,如下所示

= (SELECT DISTINCT

FROM lm_Warenkorb))运算符会对您有所帮助,因为接受一组值,如果SELECT Schwellwert FROM lm_Schwellwert WHERE (Typ IN (SELECT Typ FROM lm_Artikel WHERE `Index` IN (SELECT DISTINCT `Index` FROM lm_Warenkorb))) 等于其中一个值,则返回true

答案 1 :(得分:1)

也许in会帮助您:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ in (SELECT Typ FROM lm_Artikel WHERE `Index` in (SELECT DISTINCT `Index` FROM lm_Warenkorb)))

您还可以使用聚合函数min,例如:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ in (SELECT min(Typ) FROM lm_Artikel WHERE `Index` in (SELECT min(`Index`) FROM lm_Warenkorb)))

答案 2 :(得分:1)

试试这个

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ = (SELECT Typ FROM lm_Artikel 
              WHERE `Index` in (SELECT DISTINCT `Index` FROM lm_Warenkorb)))

因为你在索引中有两个值

答案 3 :(得分:1)

SELECT DISTINCT `Index` FROM lm_Warenkorb

这个子查询返回两行..当你使用等号时,它应该只有一行..

试试这个:

SELECT Schwellwert
FROM lm_Schwellwert
WHERE (Typ = (SELECT  Typ FROM lm_Artikel WHERE `Index` in (SELECT DISTINCT `Index` FROM lm_Warenkorb)) limit 1)

答案 4 :(得分:1)

尝试此查询

Select 
    lm_Schwellwert.Schwellwert
From lm_Schwellwert
Left join lm_Schwellwert as r 
    on r.Typ = (SELECT Typ FROM lm_Artikel WHERE `Index` = (SELECT DISTINCT `Index` FROM lm_Warenkorb))
Where r.id is not null  
group by lm_Schwellwert.Typ