在php+mysql内部连接查询中查找同名用户

gijlo24d  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(207)

我正在用php+mysql开发一个非常过时的搜索系统,现在。。。所有者让我引入两个按钮来对主查询应用过滤器。第一个按钮应该只找到名字和姓氏相同的用户,第二个按钮应该只找到姓氏相同的用户。现在atm查询使用不同的连接(inner和left确实从其他表中获取其他信息)

$query = "
  SELECT s1.*,s2.first_name,s2.last_name,s2.email,s2.id as subscriber_id,s2.phone,s2.info,a.id as agency_id,a.info as agency_info,a.invoice as agency_invoice
  FROM subscription as s1
  LEFT JOIN agency as a
  ON s1.agency=a.id
  INNER JOIN subscriber as s2
  ON s1.subscriber=s2.id
  WHERE $matches AND s1.deleted=0
  ORDER BY s1.id DESC
  LIMIT $maxid,$limit
";

一切工作正常,但如果我添加子句组用户以相同的全名,它只返回一个结果每个全名。。。
如果我有一张这样的table

+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Foo        | Bar       |
| Foo        | Bar       |
| Bar        | Foo       |
| Bar        | Foo       |
| John       | Doe       |
+------------+-----------+

它回来了

+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Foo        | Bar       |
| Bar        | Foo       |
+------------+-----------+

现在。。。我添加到查询中的子句如下(count和groupby)

$query = "
  SELECT s1.*,s2.first_name,s2.last_name,s2.email,s2.id as subscriber_id,s2.phone,s2.info,a.id as agency_id,a.info as agency_info,a.invoice as agency_invoice, Count(s2.*) AS Cnt
  FROM subscription as s1
  LEFT JOIN agency as a
  ON s1.agency=a.id
  INNER JOIN subscriber as s2
  ON s1.subscriber=s2.id
  WHERE $matches AND s1.deleted=0
  GROUP BY s2.first_name, s2.last_name HAVING Cnt>1
  ORDER BY s1.id DESC
  LIMIT $maxid,$limit
";

但我已经说过了。。。它不起作用,我也不知道为什么。。。我错在哪里?是否依赖于加入?有没有办法找到一个解决方案,或者用这个问题我需要找到另一个解决方案?请帮帮我,我试过别的办法,但没办法!:d

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题