2つの異なるテーブルまたは独立したテーブルからコンポジットデータを取得したい。 ここに私のテーブル構造があります。2つの独立したmysqlテーブルからコンポジットデータを取得
表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 |
+----+----------------------------------+
私のモトは、これらのTABLE1のキーワードを持っている表2に対象者の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、またはキーワードを得ていません。
しかし、私は次のようにフェッチ場合:
+----+----------------------------------+----+---------+
| 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 |
+----+----------------------------------+----+---------+
繰り返し行:
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);
これは、データを与えています。
@Ashraful !!:
このクエリは、正しい結果を与えています – ggupta