Mysql 如何在以下场景中获取记录
我有一张如下表:Mysql 如何在以下场景中获取记录,mysql,sql-match-all,Mysql,Sql Match All,我有一张如下表: node_name id term_name ---------------------------------------------- test1 001 physics test1 001 maths test1 001 chemistry tes
node_name id term_name
----------------------------------------------
test1 001 physics
test1 001 maths
test1 001 chemistry
test2 002 physics
test2 002 maths
给定术语名称的组合,我希望找到id集仅包含给定术语名称的所有行
例如,给定术语名称“物理和数学”,我的输出应如下所示
node_name id term_name
----------------------------------------------
test2 002 physics
test2 002 maths
Id set 001也包含化学成分,这就是为什么不应该包含它。首先,您需要解析查询以将“&”转换为SQL”或“运算符” 在PHP中: 然后: 使用L 最后: 您的查询必须采用以下格式:
select x,count(1) as total from mytable where field1 = 'term1' or field1 = 'term2' having total = 2
您的问题是:获取没有其他具有相同id但存在其他术语名称的行的所有行
一种可能的方法是:
select id, node_name
from nodes join
(select id,
count(*)
from nodes
where node_name in ('physics','math')
group by id
having count(*) = 2 // this is set when generating the query ) as eligible_nodes
on nodes.id = eligible_nodes.id
你能解释一下为什么你的结果不包括Test1001物理和Test1001数学吗?他们两人的物理和数学成绩和其他两排的成绩一样。有什么区别?@ChrisWue因为id 001也包含化学成分
select x,count(1) as total from mytable where field1 = 'term1' or field1 = 'term2' having total = 2
SELECT * FROM <table> x WHERE
term_name IN ('physics','maths') AND
NOT EXISTS (SELECT * FROM <table> WHERE id=x.id AND term_name NOT IN ('physics','maths'))
select id, node_name
from nodes join
(select id,
count(*)
from nodes
where node_name in ('physics','math')
group by id
having count(*) = 2 // this is set when generating the query ) as eligible_nodes
on nodes.id = eligible_nodes.id