选择LIKE值IN子查询

时间:2012-08-23 11:58:51

标签: mysql sql

  

可能重复:
  MySQL LIKE IN()?

我有2张桌子

atm_name_initials(初始)

KS
FW
GE
SD

atm_tasks_dit(task_name,task_given_to)

task1 KS
task2 GE
task3 GE/SD

现在我想要SELECT atm_name_initials WHERE IN LIKE atm_tasks_dit.task_given_to SELECT initial FROM atm_name_initials WHERE initial IN ( SELECT DISTINCT task_given_to FROM atm_tasks_dit WHERE on_big_project_id = 29 ) 所有不同的名字atm_name_initials.initial

这是我到目前为止所做的:

atm_tasks_dit.task_given_to

如果在SELECT initial FROM atm_name_initials WHERE initial "IS LIKE" IN '% ( SELECT DISTINCT task_given_to FROM atm_tasks_dit WHERE on_big_project_id = 29 ) %'

中建立了{{1}}值,则会搜索这段代码

我想要像

这样的东西
{{1}}

或其他......

任何帮助将不胜感激,谢谢!

3 个答案:

答案 0 :(得分:2)

使用MySQL语法提出的答案之间的混合:

SELECT DISTINCT initial
  FROM atm_tasks_dit,atm_name_initials
 WHERE task_given_to LIKE CONCAT('%',initial,'%')

以防检查SQL Fiddle code

这是另一个similar question

答案 1 :(得分:0)

试试这个:

SELECT initial 
FROM   atm_name_initials
join   atm_tasks_dit
on     initial like concat('%',task_given_to,'%')
WHERE  on_big_project_id = 29

答案 2 :(得分:0)

尝试

Select distinct ani.initial--atd.task_name,atd.task_given_to
From atm_tasks_dit atd
join atm_name_initials ani on atd.task_given_to like '%' + ani.initial + '%'
-- WHERE on_big_project_id = 29

<强>结果

初始

GE 
KS 
SD

我在Sql Server中做了,因为我没有安装MySql