gpt4 book ai didi

两次使用子查询的MySQL查询

转载 作者:行者123 更新时间:2023-11-29 00:34:29 25 4
gpt4 key购买 nike

我有这个问题

SELECT itemID, locationParentID, locationID, categoryParentID, categoryID, itemTitle, itemDetails, itemAttributes, itemPictures, itemFeatured, itemSpotlight, itemAdded 
FROM items
WHERE locationID NOT IN
(SELECT locationID
FROM sites_locations
WHERE siteID != 2) AND items.siteID IN('1,2') AND itemStatus = '1'
AND itemAdded > '1356048000'

它工作正常但是当我执行这个查询时它失败了

SELECT itemID, locationParentID, locationID, categoryParentID, categoryID, 
itemTitle, itemDetails, itemAttributes, itemPictures, itemFeatured, itemSpotlight, itemAdded FROM items
WHERE locationID NOT IN
(SELECT locationID
FROM sites_locations WHERE siteID != 2)
AND categoryID NOT IN (SELECT categoryID FROM sites_categories
WHERE siteID != 2) AND items.siteID IN('1,2') AND itemStatus = '1'
AND itemAdded > '1356048000'

这里的区别是我添加了子句

AND categoryID NOT IN(SELECT categoryID FROM sites_categories 
WHERE siteID != ".$cfg['site']['siteID'].")

知道为什么这行不通吗?

或者更好的是,一个 Join Query 来帮助优化它。

sites_categories的表结构,数据在 enter image description here

items的表结构,数据在 enter image description here

sites_locations 的表结构 enter image description here

位置表结构,数据在 enter image description here

提前致谢。

最佳答案

试试这个

SELECT i.itemID, i.locationParentID, i.locationID, i.categoryParentID, i.categoryID, 
i.itemTitle, i.itemDetails, i.itemAttributes, i.itemPictures, i.itemFeatured, i.itemSpotlight, i.itemAdded
FROM items i, sites_locations sl, sites_categories sc
WHERE i.locationID = sl.locationID
AND i.categoryID = sc.categoryID
AND i.siteID = 1 AND i.itemStatus = '1'
AND i.itemAdded > '1356048000'

它可能有一些错误或与列名相关的错误请更正这些错误

关于两次使用子查询的MySQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15016118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com