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 . 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 . 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 . 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.
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. 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:
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. 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
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
$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>";
}
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.