Home > Backend Development > PHP Tutorial > mysql 唯一查询

mysql 唯一查询

WBOY
Release: 2016-06-23 14:00:41
Original
1153 people have browsed it

需要查询两列数据中中包含$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  " ; //查?字串
Copy after login


回复讨论(解决方案)

去掉 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  " ; //查?字串
Copy after login
Copy after login

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

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

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

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

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