MySQL左连接和w/ LIKE——此查询不会运行,但匹配确实存在。

时间:2022-04-24 11:46:12

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. Looking for any suggestions or feedback:

我的查询是否遵循正确的格式?我找不到任何使用LIKE和ON的例子,所以我不确定我的查询是否是问题。寻找任何建议或反馈:

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

4 个解决方案

#1


3  

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%'

#2


1  

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

#3


1  

You can use LIKE in a join, but it sounds like what you you really want is a 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%'

#4


0  

Try this, using CONCAT(). it was helpful for me, when I was selecting the full table data:

试试这个,使用CONCAT()。当我选择全表数据时,这对我很有帮助。

 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'

#1


3  

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%'

#2


1  

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

#3


1  

You can use LIKE in a join, but it sounds like what you you really want is a 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%'

#4


0  

Try this, using CONCAT(). it was helpful for me, when I was selecting the full table data:

试试这个,使用CONCAT()。当我选择全表数据时,这对我很有帮助。

 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'