简体   繁体   English

使用mysql和php搜索逗号分隔的值

[英]Search inside comma separated values using mysql and php

My table structure is, 我的表结构是

ID    NickName    City    LookingFor
------------------------------------
1      First      City1     men
2      Second     City2    Women
3      Third      City3    men,women
4      Fourth     City4    men,women

Case 1: I want to display person those who are looking for men and women . 情况1:我想向那些正在寻找男人和女人的人展示 The required output should be, 所需的输出应该是

ID    NickName    City    LookingFor
------------------------------------
1      First      City1     men
2      Second     City2    Women
3      Third      City3    men,women
4      Fourth     City4    men,women

Case 2: I want to display person those who are looking for men . 情况2:我想向人们展示那些正在寻找男人的人 The required output should be, 所需的输出应该是

ID    NickName    City    LookingFor
------------------------------------
1      First      City1     men
3      Third      City3    men,women
4      Fourth     City4    men,women

Case 3: I want to display person those who are looking for women . 情况3:我想向人们展示正在寻找女性的人 The required output should be, 所需的输出应该是

ID    NickName    City    LookingFor
------------------------------------
1      Second     City2     women
3      Third      City3    men,women
4      Fourth     City4    men,women

I have tried using below code. 我尝试使用下面的代码。 But all the above case is not coming corrrectly. 但是上述所有情况并没有得到正确解决。

SELECT * FROM `table_name WHERE LookingFor LIKE  '%men,women%';

You can remove the where clause if someone is looking for men and women, since all record will be fetch, regardless of the gender. 如果有人在寻找男性和女性,则可以删除where子句,因为无论性别如何,所有记录都将被获取。

SELECT * FROM table_name

If you're looking for a specific gender 如果您正在寻找特定性别

SELECT *
FROM table_name
WHERE CONCAT(',', LookingFor, ',') LIKE '%,men,%';

SELECT *
FROM table_name
WHERE CONCAT(',', LookingFor, ',') LIKE '%,women,%';

Use FIND_IN_SET function, to search for a substring within a comma-separate string. 使用FIND_IN_SET函数在逗号分隔的字符串中搜索子字符串。 Here are the applicable queries, for various cases you mentioned: 对于您提到的各种情况,以下是适用的查询:

If looking for Men Or Women: 如果要寻找男人或女人:

SELECT * FROM table_name 
WHERE FIND_IN_SET('men', LookingFor) > 0 OR  
      FIND_IN_SET('women', LookingFor) > 0;

If looking for Men: 如果寻找男士:

SELECT * FROM table_name 
WHERE FIND_IN_SET('men', LookingFor) > 0;

If looking for Women: 如果寻找女性:

SELECT * FROM table_name 
WHERE FIND_IN_SET('women', LookingFor) > 0;

We can try using FIND_IN_SET here, eg the query for cities having both men and women: 我们可以在此处尝试使用FIND_IN_SET ,例如查询男性和女性均相同的城市:

SELECT *
FROM yourTable
WHERE FIND_IN_SET('men', LookingFor) > 0 AND FIND_IN_SET('women', LookingFor) > 0;

But, it is really a bad practice to store CSV data in your tables. 但是,将CSV数据存储在表中确实是一个坏习惯。 You should instead normalize your data and store one gender per record. 相反,您应该规范化数据并为每条记录存储一种性别。

Trivially, if the LookingFor column only ever stores up to two genders, and you maintain an alphabetical sort, you could check directly for the combined CSV value, eg LookingFor ,如果LookingFor列最多只能存储两个性别, 并且您按字母顺序排序,则可以直接检查合并的CSV值,例如

SELECT *
FROM yourTable
WHERE LookingFor = 'men,women';

Try below for three of your cases: 请为您的三种情况尝试以下方法:

SELECT * FROM table_name WHERE LookingFor LIKE  '%men%' 

SELECT * FROM table_name WHERE LookingFor LIKE  'men%' 

SELECT * FROM table_name WHERE LookingFor LIKE LookingFor LIKE  '%women%'

try doing in php 尝试在PHP中做

 <?php 


    $conn = mysqli_connect("localhost","root","","test");
    $sql="SELECT * FROM `stack`";
    $resultset=mysqli_query($conn,$sql);
    while($row=mysqli_fetch_assoc($resultset))
    {

        check($row);

    }

    function check($row)
    {
         if(strpos(strtolower($row['LookingFor']),'men') !== false)
         {
            echo " are you looking for men</br>";
            print($row['LookingFor']);
            echo "</br>";

        }elseif(strpos(strtolower($row['LookingFor']),'women') !== false){

           echo " are you looking for women</br>";
            print($row['LookingFor']);
            echo "</br>";


    }


    }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM