使用逗号分隔的 sql id

Using id that are comma separated sql(使用逗号分隔的 sql id)
本文介绍了使用逗号分隔的 sql id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您运行此查询

select 
    GROUP_CONCAT(p1.id) _fid,
    GROUP_CONCAT(p2.id) _mid,
    count(1)
from
    new_person as pb
        inner join
    new_person as p1 ON pb.father_id = p1.id
        inner join
    new_person as p2 ON pb.mother_id = p2.id
where
    (p1.last_name <> 'N.N.'
        and p1.last_name <> '')
        or (p2.last_name <> 'N.N.'
        and p2.last_name <> '')
group by p1.first_name , p1.last_name , p2.first_name , p2.last_name
having count(1) > 1;

你会像这样得到一个 id 列表

you get a list of id back like this

'4676,6088,4804,4968,6554,5212,5504,5810,7298,7782,6970'

有没有办法让你可以立即在其他查询中使用它,还是必须先在 php 中加载它?

Is there a way so you can immediately use it in a other query or do you have to load it in php first?

推荐答案

你可以像这样在另一个查询中使用它:

You can use it in another query like so:

SELECT M.*, P.* FROM 
(  

  SELECT GROUP_CONCAT(p1.id) _fid, GROUP_CONCAT(p2.id) _mid, count(1)
  FROM new_person AS pb
     INNER JOIN new_person AS p1 ON pb.father_id = p1.id
     INNER JOIN new_person AS p2 ON pb.mother_id = p2.id
  WHERE (
     p1.last_name <> 'N.N.'
     AND p1.last_name <> '')
     OR (p2.last_name <> 'N.N.'
     AND p2.last_name <> '')
  GROUP BY p1.first_name, p1.last_name, p2.first_name, p2.last_name
  HAVING COUNT(1) > 1

) AS M INNER JOIN new_person as p ON M._fid = p.id

请注意,我将整个查询添加到 from 语句中,别名为 M.然后你可以 JOIN M 到另一个表或从那里做任何你想做的事情.

Notice I added the entire query to the from statement with the alias as M. You can then JOIN M to another table or do whatever you want from there.

这篇关于使用逗号分隔的 sql id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)