简体   繁体   English

如何从具有给定ID的MySQL中删除记录

[英]How to delete a record from mysql which are having the given id

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) 例如现在如果我传递值9,那么它应该检查merge_combo并删除具有9的行(A1,A4这两个具有9的行)

You can do this with find_in_set() : 您可以使用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. SQL具有用于存储列表的强大数据结构-称为表。 In this case, you want a junction table, with one row per merge_id and combo item. 在这种情况下,您需要一个联结表,每个merge_id和组合项merge_id一行。

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 您可以使用Find_In_Set

delete from your_table where FIND_IN_SET(@val, merge_combo)

FIND_IN_SET(str,strlist) FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. 如果字符串str在由N个子字符串组成的字符串列表strlist中,则返回1到N范围内的值。 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. 如果第一个参数是常量字符串,第二个参数是SET类型的列,则将FIND_IN_SET()函数优化为使用位算术。 Returns 0 if str is not in strlist or if strlist is the empty string. 如果str不在strlist中或strlist为空字符串,则返回0。 Returns NULL if either argument is NULL. 如果任一参数为NULL,则返回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. 不应该使用LIKE创建复杂的SQL查询。如果您的数据库设计不好,您的应用也会很糟糕。

Instead normalize your database that comes up even against the 1NF. 而是规范化甚至针对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) 如果您知道直到3个组合值或为实现最佳实现而必须创建一个单独的带有复合键的表,则尝试将merge_combomerge_comboAmerge_comboBmerge_comboC (这样可以避免使用Null值)

PS : use the queries proposed only if you can't change your database (eg an academic exercise to test your knoweledge on queries) PS :仅当您无法更改数据库时才使用建议的查询(例如,通过一项学术练习来测试您的查询知识)

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 %';

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

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