Home > Backend Development > PHP Tutorial > 如何样查找同一姓名的数据

如何样查找同一姓名的数据

WBOY
Release: 2016-06-13 12:05:45
Original
1189 people have browsed it

怎么样查找同一姓名的数据?
表A
id     name      kemu
1        张龙    作文
2        张龙    数学
3        李四    作文
5        王五    作文
6        赵虎    数学
7        赵虎    口才
8        赵虎    珠心算

请问怎么用SQL语句查找出张三和赵虎


 多科


点击上面的搜索后显示为:
1      张龙
2      赵虎
------解决方案--------------------
drop table A;<br /><br />create table A (<br />  id int,<br />  name varchar(10),<br />  kemu1 varchar(10)<br />) charset=gbk;<br /><br />set names gbk;<br /><br />insert into A values<br />  (1, '张龙', '珠心算'),<br />  (2, '张龙', '口才'),<br />  (3, '赵虎', '珠心算'),<br />  (4, '赵虎', '作文'),<br />  (5, '王朝', '数学'),<br />  (6, '王朝', '数学'),<br />  (7, '马汉', '绘画');<br /><br />select DISTINCT A.name<br />  from A,<br />    (select name, group_concat(kemu1 order by kemu1) as klist from A group by name HAVING count(DISTINCT kemu1)>1) B<br />  where find_in_set(A.kemu1, B.klist)
Copy after login
name
张龙
赵虎

------解决方案--------------------
<br />SELECT name FROM t_xuanke<br />WHERE id in( SELECT id FROM t_xuanke GROUP BY name,kemu1 HAVING count(*)<=1 )<br />GROUP BY `name`<br />HAVING COUNT(*)>1<br />
Copy after login

1.先去掉相同名字,科目1也相同的记录
2.找出多个name的记录

lz,你这张表原本是想储存学生的选课情况,用“建对值”的设计是好的,为何后面会延伸那么多科目呢?很多复杂低效率的sql就是从不合理的表设计开始的!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template