gpt4 book ai didi

mysql IN() 和 LIKE 在一起

转载 作者:行者123 更新时间:2023-11-30 21:49:39 24 4
gpt4 key购买 nike

我知道我可以重写这个查询

SELECT * FROM [table] WHERE 
(column1 = 'dog' AND column2 = 1) OR
(column1 = 'cat' AND column2 = 2) OR
(column1 = 'mouse' AND column3 = 3) OR
(column1 = 'wolf' AND column4 = 4);

以更好的方式:

SELECT  *
FROM [table]
WHERE (column1, column2) IN (('dog', 1), ('cat', 2), ('mouse', 3),
('wolf', 4));

这个查询有没有类似的方法:

SELECT * FROM [table] WHERE 
(column1 = 'dog' AND column2 like '%red%') OR
(column1 = 'cat' AND column2 like '%blue%') OR
(column1 = 'mouse' AND like '%red%') OR
(column1 = 'wolf' AND column2 like '%green%');

最佳答案

没有。元组列表形式例如

(a,b) IN ( (2,3) , (5,7) , (11,13) )

仅用于相等比较。

但是还有其他方法可以编写查询,其中一些可能比其他方法执行得更好,这取决于合适索引的可用性……例如

SELECT t1.* FROM mytable t1 WHERE t1.column1 = 'dog'   AND t1.column2 LIKE '%red%'
UNION ALL
SELECT t2.* FROM mytable t2 WHERE t2.column1 = 'cat' AND t2.column2 LIKE '%blue%'
UNION ALL
SELECT t3.* FROM mytable t3 WHERE t3.column1 = 'mouse' AND t3.column2 LIKE '%red%'
UNION ALL
SELECT t4.* FROM mytable t4 WHERE t4.column1 = 'wolf' AND t4.column2 LIKE '%green%'

关于mysql IN() 和 LIKE 在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47819214/

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