Mysql unique query-php Tutorial

Source: Internet
Author: User
For a unique mysql Query, you need to query the unique values of $ user in two columns of data.
For example, $ user = Pig
The following data indicates sixin
Id shuozhe tingzhe
1 pig
2 dogs and pig
3 NIU Ma
4 pig

The query result is 2, 4 (removed from irrelevant and duplicated pig)
However, my query statement will query 1, 2, 4, and solve. how can I only query 2, 4?
The query statement is as follows:
$ Str = "select distinct shuozhe, tingzhe from sixin where (shuozhe = '$ user' or tingzhe =' $ user') order by id desc"; // query? String


Reply to discussion (solution)

Remove or tingzhe = '$ user'

Search all the items first, and then compare them cyclically to remove unnecessary items.

Remove or tingzhe = '$ user'

If you remove or tingzhe = '$ user', there is a data shuozhe tingzhe
Cats and pigs
This item cannot be displayed. I want it to be displayed if it contains "pig ".

Search all the items first, and then compare them cyclically to remove unnecessary items.
I don't quite understand this, but I want to say something specific.

$ Str = "select shuozhe, tingzhe from sixin where (shuozhe = '$ user' or tingzhe =' $ user') group by shuozhe, tingzhe order by id desc "; // query? String

$ Str = "select shuozhe, tingzhe from sixin where (shuozhe = '$ user' or tingzhe =' $ user') group by shuozhe, tingzhe order by id desc "; // query? String

The results are repeated, that is, the pigs, dogs, and dogs will be searched together.

Guidance ,,,,,,,,,,,,,

We recommend that you retrieve all the pigs with the two fields and make a judgment in php. you need to determine that the intersection of the two fields is equal. mysql is not easy to handle.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.