Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从mysql列中查找值_Mysql_Select - Fatal编程技术网

从mysql列中查找值

从mysql列中查找值,mysql,select,Mysql,Select,我有以下表格结构 表1: | id | allowed_user_type | | 1 | 8,9,10 | 表2: | id | user_type | | 1 | 9 | 所以,想要检查表2中的用户类型是否存在于表1中允许的用户类型中:然后返回true,否则返回false 我在和FIND\u IN\u SETMySQL语句中使用了,但没有任何帮助 谢谢 您可以使用一些regexp魔术将字段从允许的用户类型中拉出。但这不值得。您需要更改表结构 我认为在表的结构中实际上

我有以下表格结构

表1:

| id | allowed_user_type |
| 1  | 8,9,10  |
表2:

| id | user_type |
| 1  | 9         |
所以,想要检查表2中的用户类型是否存在于表1中允许的用户类型中:然后返回true,否则返回false

我在和FIND\u IN\u SETMySQL语句中使用了
,但没有任何帮助


谢谢

您可以使用一些regexp魔术将字段从
允许的用户类型中拉出。但这不值得。您需要更改表结构

我认为在表的结构中实际上有更多的数据,但是在任何情况下,如果您重新设计表1,使其具有以下结构,您将使自己的生活更加轻松:

| id | allowed_user_type |
| 1  | 8                 |
| 2  | 9                 |
| 3  | 10                |
那么您的查询就是简单的


其中Table1.allowed\u user\u type=Table2.user\u type

您的问题没有正确答案,因为该问题基于错误的内容。

你的桌子结构不对。它不是“关系型”的,此外,文本字段中有数字

这是非常糟糕的设计

如果要删除一个允许的用户类型,该怎么办?您将需要复杂的代码

我建议您停止这方面的工作,学习一些关于关系数据库的知识,然后再回到您的项目中来

正确的设计应如下所示:

Table 1:
| id | allowed_user_type |
| 1  | 8                 |
| 1  | 9                 |
| 1  | 20                |

Table 2:
| id | user_type |
| 1  | 9         |
SELECT [Table 2].id 
FROM [Table 2], [Table 1]
WHERE [Table 2].user_type in ([Table 1].allowed_user_type);
SELECT count([Table 2].id)
FROM [Table 2], [Table 1]
WHERE [Table 2].user_type in ([Table 1].allowed_user_type);
然后像这样查询:

Table 1:
| id | allowed_user_type |
| 1  | 8                 |
| 1  | 9                 |
| 1  | 20                |

Table 2:
| id | user_type |
| 1  | 9         |
SELECT [Table 2].id 
FROM [Table 2], [Table 1]
WHERE [Table 2].user_type in ([Table 1].allowed_user_type);
SELECT count([Table 2].id)
FROM [Table 2], [Table 1]
WHERE [Table 2].user_type in ([Table 1].allowed_user_type);
当用户不被允许时,该查询将返回0行;当用户被“允许”时,该查询将返回1行。您也可以像这样使用“count()”函数:

Table 1:
| id | allowed_user_type |
| 1  | 8                 |
| 1  | 9                 |
| 1  | 20                |

Table 2:
| id | user_type |
| 1  | 9         |
SELECT [Table 2].id 
FROM [Table 2], [Table 1]
WHERE [Table 2].user_type in ([Table 1].allowed_user_type);
SELECT count([Table 2].id)
FROM [Table 2], [Table 1]
WHERE [Table 2].user_type in ([Table 1].allowed_user_type);
它将返回1行,其中1个字段包含匹配行数。0表示“不允许”,1表示“允许”


如果表名或字段名中有空格,则必须使用方括号。如果你没有空格,你就不必使用它们。

FIND\u IN\u SET
适合我

select t1.id, find_in_set(user_type, allowed_user_type) > 0 found
from table1 t1 join table2 t2 using (id);


但我同意其他的海报,这是糟糕的设计。如果您必须对大量行执行此操作,则速度会非常慢,因为它无法使用索引。

请显示您尝试过的查询。您可以更改表结构吗?@mcalex我知道表结构不正确。。。但我改变了它。我也有很多事情要改变…-1你的情况。在搜索“1”时,也将通过“10”、“11”等。我同意答案的其余部分。她需要一个拆分函数,mysql dosent需要one@mcalex编辑后,这将不会传递记录中列表的第一个和最后一个值,因为没有“,”在第一行之前和最后一行之后:)@Arun Killu Mysql没有拆分,因为在正确设计的关系数据库中不需要拆分。感谢链接到SQLFIDLE。好工具。