以下场景如何获取记录

发布于 2024-12-22 05:27:36 字数 776 浏览 0 评论 0原文

我有一个如下表:

node_name              id            term_name
----------------------------------------------
test1                  001           physics 
test1                  001           maths    
test1                  001           chemistry    
test2                  002           physics    
test2                  002           maths

给定术语名称的组合,我想找到 id 集仅包含给定术语名称的所有行。

例如,给定术语名称physical & maths 我的输出应该如下所示

node_name              id            term_name
----------------------------------------------
test2                  002           physics   
test2                  002           maths

Id set 001 还包含 chemistry 这就是为什么不应该包含它。

I have a table like below :

node_name              id            term_name
----------------------------------------------
test1                  001           physics 
test1                  001           maths    
test1                  001           chemistry    
test2                  002           physics    
test2                  002           maths

Given a combination of term names I want to find all rows where the id set only contains exactly the given term names.

For example given the term names physics & maths my output should be like below

node_name              id            term_name
----------------------------------------------
test2                  002           physics   
test2                  002           maths

Id set 001 contains also chemistry that is why it should not be included.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动听の歌 2024-12-29 05:27:36

您的问题:获取不存在具有相同 id 但存在其他 term_names 的其他行的所有行

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

Your question: get all rows where no other rows with same id but other term_names exists

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'))
小鸟爱天空丶 2024-12-29 05:27:36

首先,您需要解析查询以转换“&”到 SQL 'OR' 运算符
在 PHP 中:

//Parse the query
        $arr = explode('&',$query);
    $where = '';
//get the term count
    $count = count($arr);
    foreach($arr as $value){
    $where .= "term_name = '" . $value . "' OR";
    }
    //Remove last or
    $where = rtrim($where,'OR');

然后:
最后使用 L

"select node_name ,count(1) as Total from my table where $where
group by node_name
having Total =" . $count

您的查询必须采用以下格式:

select x,count(1) as total from mytable where field1 = 'term1' or field1 = 'term2' having total = 2

first of all you need to parse your query to convert the '&' to SQL 'OR' operator
in PHP :

//Parse the query
        $arr = explode('&',$query);
    $where = '';
//get the term count
    $count = count($arr);
    foreach($arr as $value){
    $where .= "term_name = '" . $value . "' OR";
    }
    //Remove last or
    $where = rtrim($where,'OR');

then :
use L

"select node_name ,count(1) as Total from my table where $where
group by node_name
having Total =" . $count

Finally :

your query must be in this format:

select x,count(1) as total from mytable where field1 = 'term1' or field1 = 'term2' having total = 2
幻想少年梦 2024-12-29 05:27:36

一种可能的方法是:

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

One possible way to do this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文