gpt4 book ai didi

mysql - 从 2 个独立的 mysql 表中获取复合数据

转载 作者:行者123 更新时间:2023-11-28 23:17:49 25 4
gpt4 key购买 nike

我想从两个不同或独立的表中获取复合数据。这是我的表格结构。

表 1:

CREATE TABLE `keywords` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`keyword` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `keyword` (`keyword`)
)

此表中的数据如下:

+----+---------+
| id | keyword |
+----+---------+
| 2 | apple |
| 1 | Banana |
| 8 | ginger |
| 6 | grapes |
| 7 | guava |
| 10 | milk |
| 3 | onion |
| 4 | potato |
| 9 | tea |
| 5 | tomato |
+----+---------+

表2;

CREATE TABLE `Rfps` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`subject` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
)

此表中的数据如下:

+----+----------------------------------+
| id | subject |
+----+----------------------------------+
| 1 | I would like to have some banana |
| 2 | I need some tea |
| 3 | I like grapes |
| 4 | yes. Beer, Banana, Onion |
| 5 | capsicum |
+----+----------------------------------+

我的 Moto 是获取表 2 中具有表 1 关键字的主题的 ID。

我创建了一个查询:

select * from Rfps where CONCAT(subject) REGEXP (select GROUP_CONCAT(keyword SEPARATOR '|') as keyword from keywords);

结果如下:

+----+----------------------------------+
| id | subject |
+----+----------------------------------+
| 1 | I would like to have some banana |
| 2 | I need some tea |
| 3 | I like grapes |
| 4 | yes. Beer, Banana, Onion |
| 5 | capsicum |
+----+----------------------------------+

但没有得到关键字的id,或者关键字。

但是如果我像这样获取:

select Rfps.id,Rfps.subject,keywords.id,keywords.keyword from Rfps,keywords where CONCAT(subject) REGEXP (select GROUP_CONCAT(keyword SEPARATOR '|') as keyword from keywords);

这是给数据作为:

+----+----------------------------------+----+---------+
| id | subject | id | keyword |
+----+----------------------------------+----+---------+
| 1 | I would like to have some banana | 2 | apple |
| 2 | I need some tea | 2 | apple |
| 3 | I like grapes | 2 | apple |
| 4 | yes. Beer, Banana, Onion | 2 | apple |
| 1 | I would like to have some banana | 1 | Banana |
| 2 | I need some tea | 1 | Banana |
| 3 | I like grapes | 1 | Banana |
| 4 | yes. Beer, Banana, Onion | 1 | Banana |
| 1 | I would like to have some banana | 8 | ginger |
| 2 | I need some tea | 8 | ginger |
| 3 | I like grapes | 8 | ginger |
| 4 | yes. Beer, Banana, Onion | 8 | ginger |
| 1 | I would like to have some banana | 6 | grapes |
| 2 | I need some tea | 6 | grapes |
| 3 | I like grapes | 6 | grapes |
| 4 | yes. Beer, Banana, Onion | 6 | grapes |
| 1 | I would like to have some banana | 7 | guava |
| 2 | I need some tea | 7 | guava |
| 3 | I like grapes | 7 | guava |
| 4 | yes. Beer, Banana, Onion | 7 | guava |
| 1 | I would like to have some banana | 10 | milk |
| 2 | I need some tea | 10 | milk |
| 3 | I like grapes | 10 | milk |
| 4 | yes. Beer, Banana, Onion | 10 | milk |
| 1 | I would like to have some banana | 3 | onion |
| 2 | I need some tea | 3 | onion |
| 3 | I like grapes | 3 | onion |
| 4 | yes. Beer, Banana, Onion | 3 | onion |
| 1 | I would like to have some banana | 4 | potato |
| 2 | I need some tea | 4 | potato |
| 3 | I like grapes | 4 | potato |
| 4 | yes. Beer, Banana, Onion | 4 | potato |
| 1 | I would like to have some banana | 9 | tea |
| 2 | I need some tea | 9 | tea |
| 3 | I like grapes | 9 | tea |
| 4 | yes. Beer, Banana, Onion | 9 | tea |
| 1 | I would like to have some banana | 5 | tomato |
| 2 | I need some tea | 5 | tomato |
| 3 | I like grapes | 5 | tomato |
| 4 | yes. Beer, Banana, Onion | 5 | tomato |
+----+----------------------------------+----+---------+

重复行。

最佳答案

CONCAT() 将字符串放在一起。仅在一列上使用它是无用的,并且会阻止索引的潜在使用。
正则表达式也太矫枉过正了。请改用 LIKE

这个查询给出了正确的结果:

select
*
from
keywords k
join Rfps r on r.subject like concat('%', k.keyword, '%');

关于mysql - 从 2 个独立的 mysql 表中获取复合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43086290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com