我有此查询,它运行完美,但我想改进它并编写它而不使用子查询。这可能吗?尽管我已经尝试了很多,但仍无法获得正确的结果。
SELECT * FROM store_product
WHERE product_id NOT IN (
SELECT entity_id
FROM import_pack_file_element
WHERE entity_id IS NOT NULL
AND import_pack_file_id IN (135)
AND status = 'DONE') AND store_id = 65
答案 0 :(得分:3)
您可以将NOT EXISTS
或NOT IN
查询重写为反联接。我没有任何理由,但是,您为什么要这样做。反连接通常用在直接[NOT] IN
/ [NOT] EXISTS
有问题的DBMS上,而MySQL则不然。反连接是防御DBMS弱点并以牺牲可读性为代价的一种手段。
SELECT sp.*
FROM store_product sp
LEFT JOIN import_pack_file_element ipfe
ON ipfe.entity_id = sp.product_id
AND ipfe.import_pack_file_id = 135
AND ipfe.status = 'DONE'
WHERE ipfe.entity_id IS NULL;
答案 1 :(得分:2)
您可以使用LEFT JOIN和IS NULL条件
SELECT store_product.*
FROM store_product
LEFT JOIN import_pack_file_element ON store_product.product_id = import_pack_file_element.entity_id
AND import_pack_file_element.import_pack_file_id IN (135)
AND import_pack_file_element.status = 'DONE'
WHERE
import_pack_file_element.entity_id IS NULL
答案 2 :(得分:1)
您可以使用function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var myCustomFunction = debounce(function() {
console.log("Call this once after resize function completes and only once every 120ms.");
}, 120);
$(window).on('resize.off-canvas', myCustomFunction );
子句:
EXISTS
答案 3 :(得分:0)
您可以尝试使用此方法(但最好改用Subquery):
SELECT [table alias].*
FROM store_product [table alias]
LEFT JOIN import_pack_file_element [table alias] on [table alias].[key] = [table alias].[key]
WHERE [table alias].entity_id IS NOT NULL AND [table alias].import_pack_file_id IN(135) AND [table alias].status = 'DONE'
您可以根据需要修改LEFT JOIN
部分和WHERE
部分。
这也是:[key]到表的键,[table别名]到表的别名。
此外,您还可以替换IN(135)并使用等号=
。IN用于多个您需要比较或验证的元素。
,这里是参考文献:
答案 4 :(得分:-1)
我对您的桌子设计不了解,但这可能有用:
SELECT * FROM store_product x LEFT OUTER JOIN import_pack_file_element y
ON x.product_id = y.entity_id
WHERE x.product_id IS NULL
AND import_pack_file_id = 135
AND status = 'DONE'