简体   繁体   English

MySQL LEFT JOIN和ON w / Like-此查询将不会运行,但是匹配存在

[英]MySQL LEFT JOIN and ON w/ LIKE - this query will not run but the match does exist

Does my query follow correct format? 我的查询格式是否正确? I can't find any examples of using LIKE and ON, so I am not sure if my query is the problem. 我找不到使用LIKE和ON的任何示例,因此不确定我的查询是否是问题。 Looking for any suggestions or feedback: 寻找任何建议或反馈:

SELECT * 
FROM table_1 a
LEFT JOIN table_sql ON a.case_id LIKE '%45305%'

This is not correct, you need to specify which column you want to use for the JOIN, but you can do something like this 这是不正确的,您需要指定要用于JOIN的列,但是您可以执行以下操作

SELECT * from table_1 AS a
   LEFT JOIN table_sql AS b
   ON a.case_id = b.id
WHERE
   a.case_id LIKE '%45305%'

Since you want to use LIKE instead of = , is this what you want? 由于您要使用LIKE而不是= ,因此这是您想要的吗?

SELECT * FROM table_1 a
LEFT JOIN table_sql ON 
 a.case_id LIKE CONCAT('%', table_sql.case_id, '%')

See also #4420554 另请参阅#4420554

You can use LIKE in a join, but it sounds like what you you really want is a UNION: 您可以在联接中使用L​​IKE,但这听起来像您真正想要的是UNION:

SELECT case_id 
     FROM table_1 a
WHERE a.case_id LIKE '%45305%'
UNION 
SELECT case_id 
     FROM table_sql s
WHERE s.case_id LIKE '%45305%'

If you need to keep track of which table the result came from, you can do something like: 如果您需要跟踪结果来自哪个表,可以执行以下操作:

SELECT 'table_a' AS what_table, case_id 
     FROM table_1 a
WHERE a.case_id LIKE '%45305%'
UNION 
SELECT 'table_b', case_id 
     FROM table_sql s
WHERE s.case_id LIKE '%45305%'

Try this, using CONCAT() . 使用CONCAT()尝试此操作。 it was helpful for me, when I was selecting the full table data: 当我选择完整表数据时,这对我很有帮助:

 SELECT p.post_name,p.post_title,p.post_type,cl.*
FROM ".$wpdb->posts." AS p
LEFT JOIN clicks AS cl
ON cl.podcast_url LIKE CONCAT('%',p.post_name,'%')
WHERE p.post_type = 'team'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM