#1235 - 这个版本的MySQL还没有支持' LIMIT& IN / ALL / ANY / SOME子查询'

时间:2014-08-03 20:51:12

标签: mysql

嗨,所以我试图将所有书籍都放到客户尚未订购的系列中。我目前的理论是,最好的方法是找到他们订购的书系列。然后构建这些系列中所有书籍的列表,然后找到该列表中尚未订购的所有书籍。然而,在我试图说出凌乱的三重查询时,我只得到了第二部分后得到了上述错误。到目前为止,这是我的查询:

SELECT books.bookid
FROM books, bookseries, series
WHERE books.bookID=bookseries.bookID 
      AND bookseries.bookID=series.seriesID
      series.seriesID IN (SELECT series.seriesID, COUNT(series.seriesID) as cnt
                          FROM customers, orders, orderlines, books, bookseries, series
                          WHERE customers.custID=1
                                AND customers.custID=orders.custID
                                AND orders.orderID=orderlines.orderID
                                AND orderlines.bookID=books.bookID
                                AND books.bookID=bookseries.bookID
                                AND bookseries.seriesID=series.seriesID
                          GROUP BY series.seriesID
                          ORDER BY cnt desc LIMIT 1)

我知道内部查询有效;只有它返回两列,seriesID和cnt,我认为这可能是我的问题,但我不确定。我也看到很多人都有这个问题,加入通常是有效的,但我对他们不太了解。有人可以帮助/教我吗?也许建议一个更好的方法?速度并不是太重要,这是为了测试一些我正在研究的有趣项目。非常感谢你!

好的,如果我做错了,请告诉我,但是:http://sqlfiddle.com/#!2/6816c/1是数据库的样子。我正在尝试获得与我认识的客户喜欢并且他们不拥有的同一系列书籍的输出。所以对于用户1我知道他们有哈利波特1,2,6,7。所以我的查询会发现他们喜欢哈利波特系列并输出他们不拥有的头衔,在这种情况下是哈利波特3,4, 5.或阿兹卡班,火焰杯和凤凰社。

总结:第一步:找到客户拥有书籍的所有系列。第二步:查找所有系列中的所有书籍。第三步:找到该列表中客户不拥有的图书。

2 个答案:

答案 0 :(得分:1)

SELECT b.bookID, b.title
FROM books AS b
INNER JOIN bookseries AS bs ON bs.bookID = b.bookID AND bs.seriesID IN (
  SELECT series.seriesID
  FROM customers, orders, orderlines, books, bookseries, series
  WHERE customers.custID=1
        AND customers.custID=orders.custID
        AND orders.orderID=orderlines.orderID
        AND orderlines.bookID=books.bookID
        AND books.bookID=bookseries.bookID
        AND bookseries.seriesID=series.seriesID
  GROUP BY series.seriesID)
WHERE b.bookID NOT IN (
  SELECT ol.bookID
  FROM orders AS o
  INNER JOIN orderlines AS ol ON ol.orderID = o.orderID
  INNER JOIN books AS b ON b.bookID = ol.bookID
  WHERE o.custID = 1
)

好的格式有点奇怪,但这是我最后的查询。非常感谢@ hex494D49让我走上正轨!

答案 1 :(得分:0)

获取尚未由客户订购的系列(1)的书籍(1)

SELECT b.bookID, b.title
FROM books AS b
INNER JOIN bookseries AS bs ON bs.bookID = b.bookID AND bs.seriesID = 1
WHERE b.bookID NOT IN (
  SELECT ol.bookID
  FROM orders AS o
  INNER JOIN orderlines AS ol ON ol.orderID = o.orderID
  INNER JOIN books AS b ON b.bookID = ol.bookID
  WHERE o.custID = 1
)

或者

SELECT b.bookID, b.title
FROM books AS b
INNER JOIN bookseries AS bs ON bs.bookID = b.bookID AND bs.seriesID = 1 
LEFT JOIN orderlines AS ol ON ol.bookID = b.bookID
LEFT JOIN orders AS o ON o.orderID = ol.orderID AND o.custID = 1
WHERE ol.bookID IS NULL

PHP中的用法:

$rs = mysqli_query("SELECT b.bookID, b.title
FROM books AS b
INNER JOIN bookseries AS bs ON bs.bookID = b.bookID AND bs.seriesID = " . $_GET["seriesID"] . "
WHERE b.bookID NOT IN (
  SELECT ol.bookID
  FROM orders AS o
  INNER JOIN orderlines AS ol ON ol.orderID = o.orderID
  INNER JOIN books AS b ON b.bookID = ol.bookID
  WHERE o.custID = " . $_GET["custID"] . "
)");

while($b = mysqli_fetch_array($rs, MYSQLI_ASSOC)){
    echo $b["bookID"] . " - " . $b["title"] . "<br />";
}