欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

mysql 唯一查询

程序员文章站 2022-06-08 23:09:21
...
需要查询两列数据中中包含$user的唯一值
例如 $user=猪
有以下数据,表明为sixin
id shuozhe tingzhe
1 猪 狗
2 狗 猪
3 牛 马
4 猪 牛

需要查询结果为2,4,(去除和猪无关和重复的)
但是我的查询语句会查询出1,2,4,求解,怎么才能只查询出2,4
查询语句如下
$str="select distinct shuozhe,tingzhe from sixin where (shuozhe='$user' or tingzhe='$user') order by  id desc  " ; //查?字串


回复讨论(解决方案)

去掉 or tingzhe='$user'

先全部搜出来,然后循环对比,把不要的去掉。

去掉 or tingzhe='$user'

如果去掉 or tingzhe='$user' ,有一条数据 shuozhe tingzhe
猫 猪
这条就无法显示出来,我是想要,包含“猪“的都会显示

先全部搜出来,然后循环对比,把不要的去掉。
这个不太理解,希望能说的具体点

$str="select shuozhe,tingzhe from sixin where (shuozhe='$user' or tingzhe='$user') group by shuozhe,tingzhe order by  id desc  " ; //查?字串

$str="select shuozhe,tingzhe from sixin where (shuozhe='$user' or tingzhe='$user') group by shuozhe,tingzhe order by  id desc  " ; //查?字串

这个的结果还是有重复的,就是 猪,狗和狗,猪会一起查询出来

求指导,,,,,,,,,,,,,

建议还是把两个字段的所有为 猪 的都取出来 在php判断吧 你这查询要判断交叉式相等了 mysql不好处理

相关标签: mysql 唯一查询