一个复杂应用,现在我抽象出来最简单的应用看如何使用sql来解决
一个表里a (id name email phone link)
如果一条记录name能在a表里找到相同的值,那么该条记录加上20分;
同理email。phone也e能在a表里找到相同的值,那么该条记录也各加上20分(这个比较复杂,现这里先不考虑如何将该表分组)
假设现在得到一个分组如下, 这里新加了一个字段link
这link用于标识同组的其他的id。比如id=1 ,那么link=2,3,4。 其他类似问sql能否做到下面例子的link??id name email phone link
1 li [email protected] 1122 2,3,4 60
2 li [email protected] 2222 1,3,4 60
3 li [email protected] 2222 1,2,4 60
4 ly [email protected] 1122 1,2,3 40(ly不能找到相同的哦)
一个表里a (id name email phone link)
如果一条记录name能在a表里找到相同的值,那么该条记录加上20分;
同理email。phone也e能在a表里找到相同的值,那么该条记录也各加上20分(这个比较复杂,现这里先不考虑如何将该表分组)
假设现在得到一个分组如下, 这里新加了一个字段link
这link用于标识同组的其他的id。比如id=1 ,那么link=2,3,4。 其他类似问sql能否做到下面例子的link??id name email phone link
1 li [email protected] 1122 2,3,4 60
2 li [email protected] 2222 1,3,4 60
3 li [email protected] 2222 1,2,4 60
4 ly [email protected] 1122 1,2,3 40(ly不能找到相同的哦)
max(if(b.name=a.name,20,0)) +
max(if(b.email=a.email,20,0))+
max(if(b.phone=a.phone,20,0))
from yourTable a,yourTable b
where find_in_set(a.id,b.link)
group by a.id
1 li [email protected] 1122 2,3,4
2 li [email protected] 2222 1,3,4
3 li [email protected] 2222 1,2,4
4 ly [email protected] 1122 1,2,3
from yourTable a,yourTable b
where (a.name=b.name or b.email=a.email or b.phone=a.phone)
and a.id!=b.id
group by a.id
'截至2009-09-03 18:40:33 用户结帖率60.66% 未结帖:155 楼主啊,你的未结贴也太多了。 155 个贴,一20s (随机给分或不满意结贴)也得结上个50分钟。如果打算认真结估计半天啊。
建议问题解决的时候及时结贴。
from wy a,wy b
where (a.name=b.name or b.email=a.email or b.phone=a.phone)
and a.id!=b.id
group by a.id;
+----+--------------------+
| id | group_concat(b.id) |
+----+--------------------+
| 1 | 3,4,2 |
| 2 | 3,4,1 |
| 3 | 2,1 |
| 4 | 2,1 |
+----+--------------------+
4 rows in set奇怪,3,4 少了点
y;
+-------+-----------+------+----+
| phone | email | name | id |
+-------+-----------+------+----+
| 1122 | [email protected] | li | 1 |
| 2222 | [email protected] | li | 2 |
| 2222 | [email protected] | li | 3 |
| 1122 | [email protected] | ly | 4 |
+-------+-----------+------+----+
4 rows in set
这样产生若干个分组,我必须在前面加上新字段pid
这里pid=1 表示是第1组。
在3万条记录里 估计有20多个这样的分组
我该用在sql里如何作才能实现这个pid功能 (组标志)pid| id | group_concat(b.id) |
+----+--------------------+
1| 1 | 3,4,2 |
1| 2 | 3,4,1 |
1| 3 | 2,1 |
1| 4 | 2,1 |
+----+--------------------+ mysql> select a.id,group_concat(b.id)
from wy a,wy b
where (a.name=b.name or b.email=a.email or b.phone=a.phone)
and a.id!=b.id
group by a.id;
+----+--------------------+
| id | group_concat(b.id) |
+----+--------------------+
| 1 | 3,4,2 |
| 2 | 3,4,1 |
| 3 | 2,1 |
| 4 | 2,1 |
+----+--------------------+