简体   繁体   English

当相关记录在MySQL中包含给定值时,如何从联接表中提取所有数据?

[英]How do you pull all data from a joined table when a related record contains a given value in MySQL?

I have created the following query: 我创建了以下查询:

SELECT wvwcs1.*, wvwcs2.* FROM wvwcs1

inner join wvwcs2 on wvwcs1.id = wvwcs2.wvwcs1_id
inner join wvwcs2 w2 on wvwcs1.id = wvwcs2.wvwcs1_id AND wvwcs2.value LIKE '%ee%'

My tables are created like so: 我的表是这样创建的:

CREATE TABLE `wvwcs1` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `form_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1 COMMENT='      ';

CREATE TABLE `wvwcs2` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `wvwcs1_id` int(10) unsigned NOT NULL,
  `value` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=latin1;

The data, as an example just using two records: 以数据为例,仅使用两个记录:

mysql> select * from wvwcs1;
+----+---------+
| id | form_id |
+----+---------+
|  1 |      23 |
|  2 |      24 |
+----+---------+
2 rows in set (0.00 sec)



mysql> select * from wvwcs2;
+----+-----------+-------+
| id | wvwcs1_id | value |
+----+-----------+-------+
|  1 |         1 | aa    |
|  2 |         1 | bb    |
|  3 |         1 | cc    |
|  4 |         2 | aa    |
|  5 |         2 | cc    |
|  6 |         2 | dd    |
|  7 |         2 | ee    |
+----+-----------+-------+
2 rows in set (0.00 sec)

What I need to do, is to select all the records from wvwcs2 where ONE of the records in wvwcs2 is a given value. 我需要做的是从wvwcs2中选择所有记录,其中wvwcs2中的一条记录是给定值。 So, assume that we want to select records where wvwcs2.value = dd . 因此,假设我们要选择wvwcs2.value = dd记录。 In this case, we would get returned: 在这种情况下,我们将返回:

+----+-----------+-------+
| id | wvwcs1_id | value |
+----+-----------+-------+
|  4 |         2 | aa    |
|  5 |         2 | cc    |
|  6 |         2 | dd    |
|  7 |         2 | ee    |
+----+-----------+-------+

When I run this query currently, it gives me all records. 当前运行此查询时,它会提供所有记录。 I thought I would need to do an inner join on the same table, but I must be off. 我以为我需要在同一张桌子上做一个内部联接,但是我必须离开。 I am stumped. 我感到难过。

Try this: 尝试这个:

SELECT A.*, C.form_id
FROM wvwcs2 A JOIN 
(SELECT DISTINCT wvwcs1_id FROM wvwcs2 WHERE LOWER(`value`) LIKE '%dd%') B
ON A.wvwcs1_id=B.wvwcs1_id
LEFT JOIN wvwcs1 C 
ON A.wvwcs1_id=C.id;

See Demo on SQL Fiddle . 请参阅SQL Fiddle上的演示

Probably this is what you want here: 这可能是您想要的:

SELECT A.*, B.*
FROM wvwcs1 A JOIN wvwcs2 B ON A.id = B.wvwcs1_id
WHERE A.id IN
(SELECT wvwcs1_id FROM wvwcs2 WHERE value like '%ee%');

See Demo on SQL Fiddle (based on cdaigas initial work) 请参阅有关SQL Fiddle的演示 (基于cdaigas的初始工作)

After the first comment the easiest solution seems to be the following (no need to join anything here at all): 发表第一个评论后,最简单的解决方案似乎是以下操作(根本不需要在此处加入任何内容):

SELECT * FROM wvwcs2 WHERE wvwcs1_id IN 
   (SELECT wvwcs1_id FROM wvwcs2 WHERE value like '%ee%');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何显示来自mysql表中多个条目的数据,这些条目通过单个值匹配与另一个联接? - How do you display data from multiple entries in a mysql table that are JOINED with another via a single value match? 如何使用SQLite基于相关表值从一组表中提取数据? - How do I pull out data from a group of tables based on an related table value with SQLite? 如何检查表是否包含数据? (Python Flask MYSQL) - How do you check if a table contains data? (Python Flask MYSQL) Mysql + php,如何创建一个链接,当您按“标题”时显示记录中的所有数据 - Mysql+php, how to make a link that shows all data from a record when you press the “headline” mysql从联接表中删除记录 - mysql delete record from joined table 如何在没有一条记录的情况下显示表中的所有值 - How to to display all value from table without one record MySQL MYSQL一个表中的所有记录,只有另一表中的最后一个连接的记录 - MYSQL All records from one table and only the last joined record from other table 如果从另一个表中加入一个名称,如果该名称中包含重复项,则在该记录中显示 - Display one name in a record if it contains duplicates when joined from another table MySQL从特定的相关记录中获取所有数据 - MySQL get all data from specific related record 当表列连接两次时,从MySQL中回显数据 - echoing data From mySQL when table column joined twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM