简体   繁体   English

如何从mysql表中选择所有数据,其中列是数组或单个,我的值是否与此数组的一个项匹配?

[英]how to select all data from mysql table where column is an array or single and my value match with one item of this array?

Please could someone help me? 请有人帮帮我吗? I want to select all values in mysql table where the column that i want to check get a value is mix with aingle or array of values....... So to be more clear I have a table to store all messages from many sender to one or many reciever.... 我想选择mysql表中的所有值,我要检查的列得到的值是一个值或一组数值.......所以更清楚我有一个表来存储来自许多的所有消息发送者到一个或多个接收者....

my functions is 我的职责是

public static function find_messagesTo_by_user_id($mess_to=0) {
          global $database;
          $mess_to = $database->escape_value($mess_to);
          $sql  = "SELECT * FROM ".self::$table_name;
          $sql .= " WHERE mess_to = '{$mess_to}'";
          $sql .= " AND mess_deleted = 0";
          $sql .= " ORDER BY mess_created_date DESC";
          $result_array = parent::find_by_sql($sql);
          return $resultrray;
      }

So 'mess_to ' has array and single value .... they are only numbers Like (1, 15, 25 ,26 ,27 , array(1,25, 27) , 31, 42, .......) 所以'mess_to'有数组和单值....它们只是数字像(1,15,25,26,27,数组(1,25,27),31,42,.......)

Please, i break my head on it :) 拜托,我打破了我的头脑:)

I waiting for any help? 我在等什么帮忙?

Building on @Maluchi's answer. 以@ Maluchi的答案为基础。 Make sure your data looks like: 确保您的数据如下:

| mess_to         |
+-----------------+
| ,123,           |
| ,123,456,152,1, |
| ,456,567,       |
| ,3,             |

So surround each value in , . 所以围绕每个值, then you can safely do: 然后你可以安全地做:

WHERE `mess_to` LIKE "%,{$mess_to},%"

This ensures that $mess_to = 1 will match only the 2nd row, and not the 1st as well. 这样可以确保$mess_to = 1仅匹配第2行,而不是第1行。


You could also denormalize your data and make a table to JOIN on. 您还可以对数据进行非规范化,并将表格设置为JOIN

If I'm reading it correctly, $mess_to is passed into the function and could contain either a single value or it could be passed in an array. 如果我正确读取它,$ mess_to将传递给函数,并且可以包含单个值,也可以在数组中传递。

When matching multiple values, the SQL should be looking for a comma-separated list. 当匹配多个值时,SQL应该查找以逗号分隔的列表。 The where clause needs to be IN the list rather than EQUAL to the list. where子句需要在列表中而不是列表中的EQUAL。

Try: 尝试:

public static function find_messagesTo_by_user_id($mess_to=0) {
      global $database;
      $mess_to = $database->escape_value($mess_to);
      $sql  = "SELECT * FROM ".self::$table_name;
      $sql .= " WHERE mess_to IN (" . implode(',', $mess_to) . ")";
      $sql .= " AND mess_deleted = 0";
      $sql .= " ORDER BY mess_created_date DESC";
      $result_array = parent::find_by_sql($sql);
      return $resultrray;
  }

See this line in particular: 特别看到这一行:

$sql .= " WHERE mess_to IN (" . implode(',', $mess_to) . ")";

Code edited with geomagas's comments! 代码编辑与geomagas的评论! (Thank you geomagas!) (谢谢geomagas!)

asuming your column is like this 认为你的专栏是这样的

| mess_to |
+---------+
| 1       |
| 1,2,3,4 |
| 2       |
| 3       |

you can use the LIKE operator: 你可以使用LIKE运算符:

$sql .= " WHERE mess_to LIKE '%{$mess_to}%'";

this will match every row where mess_to has the string value of $mess_to (your column data type should be string for this to work). 这将匹配mess_to具有字符串值$mess_to每一行(您的列数据类型应为字符串以使其工作)。

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

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