字符串中字符串查询?

发布于 2024-11-05 02:15:29 字数 314 浏览 6 评论 0原文

我有这个数据库,我在其中存储了一些标签。 我像这样存储标签:

"humor,funny,animal"

现在我需要一个 mysql 查询,当我搜索“幽默”、“有趣”或“动物”时,它会选择这一行。到目前为止我所拥有的:

SELECT id FROM database WHERE tags REGEXP 'humor' LIMIT 1

不幸的是,它不起作用。你们中有人可以帮我吗?

编辑:感谢您的所有回复!我现在需要先研究这个!但问题解决了:)

I have this database where I have stored some tags in it.
I stored the tags like this:

"humor,funny,animal"

Now I need a mysql query that selects this line when I search for "humor", "funny" or "animal". What I have until now:

SELECT id FROM database WHERE tags REGEXP 'humor' LIMIT 1

Unfortunately, it does not work. Could someone of you please help me out?

Edit: Thanks for all the responses! I will now need to study this first! But problem solved :)

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

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

发布评论

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

评论(3

ぶ宁プ宁ぶ 2024-11-12 02:15:29

短期

因为标签存储为非规范化数据,所以使用 FIND_IN_SET 函数

SELECT t.id
  FROM YOUR_TABLE t
 WHERE FIND_IN_SET('humour', t.tags) > 0

长期解决方案

设置表以正确处理多对多关系:

TAGS

  • tag_id(主键)
  • tag_description

ITEMS

  • item_id(主键) key)

ITEM_TAGS

  • item_id (主键,ITEMS.item_id 的外键)
  • tag_id (主键,TAGS.tag_id 的外键)

将 ITEM_TAGS 中的两列作为主键意味着您不必担心重复。是的,这意味着使用 InnoDB 引擎...

然后,您可以使用:

SELECT i.item_id
  FROM ITEMS i
 WHERE EXISTS (SELECT NULL
                 FROM ITEM_TAGS it
                 JOIN TAGS t ON t.tag_id = it.tag_id
                WHERE t.tag_description = 'humour'
                  AND it.item_id = i.item_id)

Short Term

Because the tags are stored as denormalized data, use the FIND_IN_SET function:

SELECT t.id
  FROM YOUR_TABLE t
 WHERE FIND_IN_SET('humour', t.tags) > 0

Long Term Solution

Setup the tables to properly handle a many-to-many relationship:

TAGS

  • tag_id (primary key)
  • tag_description

ITEMS

  • item_id (primary key)

ITEM_TAGS

  • item_id (primary key, foreign key to ITEMS.item_id)
  • tag_id (primary key, foreign key to TAGS.tag_id)

Making the two columns in ITEM_TAGS the primary key means you don't have to worry about duplicates. And yes, this means using the InnoDB engine...

Then, you can use:

SELECT i.item_id
  FROM ITEMS i
 WHERE EXISTS (SELECT NULL
                 FROM ITEM_TAGS it
                 JOIN TAGS t ON t.tag_id = it.tag_id
                WHERE t.tag_description = 'humour'
                  AND it.item_id = i.item_id)
安穩 2024-11-12 02:15:29

您可以使用 LIKE

 SELECT id FROM database WHERE tags LIKE '%humor%' LIMIT 1

它将搜索“humor”为子字符串的任何条目。请注意,这还将返回标记为“幽默”的项目。

但正如其他人所说,最好有一个单独的标签表。为此,您还需要一个数据透视表。

例如

--------------  data  -------------
|     ID         |     NAME       | 
| 1              | example        |
| 2              | example 2      | 
-----------------------------------

--------------  tags  -------------
|      ID        |     NAME       |
| 1              | humor          |
| 2              | cats           |
| 3              | wumpus         |
-----------------------------------

------------  data_tags  ----------
|   DATA_ID      |   TAG_ID       |
| 1              | 1              |
| 1              | 2              |
| 2              | 1              |
| 2              | 3              |
-----------------------------------

You can use LIKE

 SELECT id FROM database WHERE tags LIKE '%humor%' LIMIT 1

Which will search for any entry where 'humor' is a substring. Note this will also return items tagged 'humorous'.

But like others said, having a separate table for tags would be best. To do this you will also need a pivot table.

So for example

--------------  data  -------------
|     ID         |     NAME       | 
| 1              | example        |
| 2              | example 2      | 
-----------------------------------

--------------  tags  -------------
|      ID        |     NAME       |
| 1              | humor          |
| 2              | cats           |
| 3              | wumpus         |
-----------------------------------

------------  data_tags  ----------
|   DATA_ID      |   TAG_ID       |
| 1              | 1              |
| 1              | 2              |
| 2              | 1              |
| 2              | 3              |
-----------------------------------
流云如水 2024-11-12 02:15:29

为了扩展 Tomalak 的评论,最好使用数据库标签关系的多对多关系来解决这个问题。这涉及添加两个新表。像这样的东西(原谅我生锈的MySQL)

CREATE TABLE `Tag` (
    id  INT(11)     UNSIGNED NOT NULL AUTO_INCREMENT,
    tag VARCHAR(64) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE      (tag)
) ENGINE=InnoDB;

CREATE TABLE `DatabaseTag` (
    database_id INT(11) UNSIGNED NOT NULL, -- just guessing your database.id type here
    tag_id      INT(11) UNSIGNED NOT NULL,
    PRIMARY KEY (database_id, tag_id),
    FOREIGN KEY (database_id) REFERENCES `database` (id)
        ON DELETE CASCADE,
    FOREIGN KEY (tag_id)      REFERENCES `Tag` (id)
        ON DELETE CASCADE
) ENGINE=InndoDB;

然后,要查找与标签“幽默”匹配的所有数据库记录,您的查询将如下所示

SELECT id FROM `database` d
INNER JOIN `DatabaseTag` dt ON d.id = dt.database_id
INNER JOIN `Tag` t ON dt.tag_id = t.id
WHERE t.tag = 'humour'

To expand on Tomalak's comment, this would be best solved using a many-to-many relationship for database to tag relationships. This involves adding two new tables. Something like this (forgive my rusty MySQL)

CREATE TABLE `Tag` (
    id  INT(11)     UNSIGNED NOT NULL AUTO_INCREMENT,
    tag VARCHAR(64) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE      (tag)
) ENGINE=InnoDB;

CREATE TABLE `DatabaseTag` (
    database_id INT(11) UNSIGNED NOT NULL, -- just guessing your database.id type here
    tag_id      INT(11) UNSIGNED NOT NULL,
    PRIMARY KEY (database_id, tag_id),
    FOREIGN KEY (database_id) REFERENCES `database` (id)
        ON DELETE CASCADE,
    FOREIGN KEY (tag_id)      REFERENCES `Tag` (id)
        ON DELETE CASCADE
) ENGINE=InndoDB;

Then, to find all the database records matching tag "humour", your query would look like

SELECT id FROM `database` d
INNER JOIN `DatabaseTag` dt ON d.id = dt.database_id
INNER JOIN `Tag` t ON dt.tag_id = t.id
WHERE t.tag = 'humour'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文