Now i have the table like
--------------------------
merge_id | merge_combo |
--------------------------
A1 | 25,9 |
A2 | 25,21 |
A3 | 2,5,15 |
A4 | 2,9,5 |
A5 | 12,19,2 |
A6 | 2,1 |
--------------------------
for example now if i pass the value 9, then it should check the merge_combo and delete the rows which are having 9 (A1,A4 this two having 9)
You can do this with find_in_set()
:
delete from tbl
where find_in_set(9, merge_combo) > 0;
The bigger issue is that you are storing numbers in a string for a list. SQL has this great data structure for storing lists -- it is called a table. In this case, you want a junction table, with one row per merge_id
and combo item.
DELETE from your_table where FIND_IN_SET(9, merge_combo) or merge_combo IN(9)
您可以使用以下qry
DELETE FROM tbl WHERE merge_combo LIKE '%,9,%' OR merge_combo LIKE '9,%' OR merge_combo LIKE '%,9' OR merge_combo = '9';
You may use Find_In_Set
delete from your_table where FIND_IN_SET(@val, merge_combo)
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.
使用以下查询
DELETE FROM tbl WHERE FIND_IN_SET("9",merge_combo)
You should not create a complex SQL query with LIKE
.If you have a bad db design your app will be bad too.
Instead normalize your database that comes up even against the 1NF. Try to break merge_combo
to merge_comboA
and merge_comboB
and merge_comboC
if you know that you will have to untill 3 combo values or for the best implementation create a separate table with composite keys (this way you will avoid Null values)
PS : use the queries proposed only if you can't change your database (eg an academic exercise to test your knoweledge on queries)
Connection:
$mysqli = new mysqli ('localhost', 'admin_user', 'Paradise1191', 'admin_db');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->set_charset("utf8");
Select & Delete:
$var = "Your Result";
$array = array();
$query = "SELECT * FROM `TABLE` WHERE 1";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_array())
{
$this_id = $row['merge_id'];
$var1 = $row['merge_combo'];
$array = explode(",",$var1 );
if (in_array($var, $people)) {
$query = "DELETE FROM `TABLE` WHERE `merge_id` = '$this_id'";
$mysqli->query($query);
}
}
$result->close();
}
您应该尝试以下查询:
DELETE FROM merge WHERE merge_combo LIKE '% 9 %';
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.