[英]How to use mysql query to select only rows where column value contains value
I have a simple query as below.我有一个简单的查询,如下所示。 Bit new to all this.
对这一切都有些陌生。 I am selecting rows that contain specific words in one column - location column has values like 'Spain>Canary Islands>Lanzarote>Playa Lanzarote'.
我正在选择在一列中包含特定单词的行 - 位置列的值类似于“西班牙>加那利群岛>兰萨罗特岛>兰萨罗特岛海滩”。
I can find rows that contain a specific value but I need to refine it so it only matches rows where the 3rd value after each '>' is the same, rather than the whole value.我可以找到包含特定值的行,但我需要对其进行优化,以便它只匹配每个“>”之后的第三个值相同的行,而不是整个值。
$location = 'Lanzarote';
$results = $wpdb->get_results($wpdb->prepare("
SELECT *
FROM $tablename
WHERE location LIKE '%%$location%%'
ORDER BY rating
LIMIT 10
"));
Does anyone know how to do this?有谁知道如何做到这一点? I have spent hours searching and trying various things.
我花了几个小时搜索和尝试各种事情。 I thought SUBSTRING_INDEX might work but no luck as this seems to apply to the result set.
我认为 SUBSTRING_INDEX 可能会起作用,但没有运气,因为这似乎适用于结果集。
SELECT *
FROM $tablename
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(location, '>', 4), '>', -1) LIKE '%$location%'
ORDER BY rating
LIMIT 10
If there is less than 4 parts in a path (less than 3 '>'
symbols) then the last part will be tested.如果路径中的部分少于 4 个(少于 3 个
'>'
符号),则将测试最后一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.