如何从 MySQL 中选择一个 SET 不等于某个值的地方?

发布于 2024-11-28 15:09:26 字数 186 浏览 0 评论 0原文

我们遇到了一个问题,因为这就是表格的设置方式。我们如何从集合不包含值的表中进行选择?如果我们有一个包含一、二的集合,并且我们这样做了,

SELECT *
FROM table
WHERE column NOT IN('one')

我们仍然会得到该行。正确的语法是什么?

谢谢

we have an issue because this is how the table was setup. How do we select from a table where a set does not contain a value? If we have a set with one,two and we do

SELECT *
FROM table
WHERE column NOT IN('one')

we will still get that row. What is the correct syntax for this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你又不是我 2024-12-05 15:09:26

这是有效的:

SELECT *, FIND_IN_SET('hidden', props) As hidden
FROM gt_content
HAVING hidden IS NULL

this is what worked:

SELECT *, FIND_IN_SET('hidden', props) As hidden
FROM gt_content
HAVING hidden IS NULL
自由如风 2024-12-05 15:09:26

尝试:

SELECT *
FROM table
WHERE FIND_IN_SET('one',column) = 0

我做了一些测试:

mysql> CREATE TABLE setTest (s SET('a','b','c','d'));
Query OK, 0 rows affected (0.60 sec)

mysql> INSERT INTO setTest VALUES ('a'),('a,b'),('b'),('a,c'),('c,d');
Query OK, 5 rows affected (0.14 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM setTest;
+------+
| s    |
+------+
| a    |
| a,b  |
| b    |
| a,c  |
| c,d  |
+------+
5 rows in set (0.20 sec)

mysql> SELECT * FROM setTest WHERE FIND_IN_SET('a',s) = 0;
+------+
| s    |
+------+
| b    |
| c,d  |
+------+
2 rows in set (0.12 sec)

mysql> SELECT * FROM setTest WHERE FIND_IN_SET('b',s) = 0;
+------+
| s    |
+------+
| a    |
| a,c  |
| c,d  |
+------+
3 rows in set (0.00 sec)


mysql> SELECT * FROM setTest WHERE FIND_IN_SET('b',s) > 0;
+------+
| s    |
+------+
| a,b  |
| b    |
+------+
2 rows in set (0.04 sec)

Try:

SELECT *
FROM table
WHERE FIND_IN_SET('one',column) = 0

Some test I did:

mysql> CREATE TABLE setTest (s SET('a','b','c','d'));
Query OK, 0 rows affected (0.60 sec)

mysql> INSERT INTO setTest VALUES ('a'),('a,b'),('b'),('a,c'),('c,d');
Query OK, 5 rows affected (0.14 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM setTest;
+------+
| s    |
+------+
| a    |
| a,b  |
| b    |
| a,c  |
| c,d  |
+------+
5 rows in set (0.20 sec)

mysql> SELECT * FROM setTest WHERE FIND_IN_SET('a',s) = 0;
+------+
| s    |
+------+
| b    |
| c,d  |
+------+
2 rows in set (0.12 sec)

mysql> SELECT * FROM setTest WHERE FIND_IN_SET('b',s) = 0;
+------+
| s    |
+------+
| a    |
| a,c  |
| c,d  |
+------+
3 rows in set (0.00 sec)


mysql> SELECT * FROM setTest WHERE FIND_IN_SET('b',s) > 0;
+------+
| s    |
+------+
| a,b  |
| b    |
+------+
2 rows in set (0.04 sec)
嗳卜坏 2024-12-05 15:09:26

来自 mysql 手册 http://dev.mysql.com/doc/ refman/5.0/en/set.html

通常,您使用 FIND_IN_SET() 函数或 LIKE 运算符搜索 SET 值:

  SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0;
  SELECT * FROM tbl_name WHERE set_col LIKE '%value%';

第一个语句查找 set_col 包含值集成员的行。第二个类似,但不相同:它查找 set_col 在任何地方包含值的行,即使是另一个集合成员的子字符串。

From the mysql manual at http://dev.mysql.com/doc/refman/5.0/en/set.html:

Normally, you search for SET values using the FIND_IN_SET() function or the LIKE operator:

  SELECT * FROM tbl_name WHERE FIND_IN_SET('value',set_col)>0;
  SELECT * FROM tbl_name WHERE set_col LIKE '%value%';

The first statement finds rows where set_col contains the value set member. The second is similar, but not the same: It finds rows where set_col contains value anywhere, even as a substring of another set member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文