简体   繁体   English

如何使用SELECT查询从两个表中输出结果-查询逻辑以限制一个表中的输出数据

[英]How to use a SELECT query to output results from two tables - Query logic to limit output data from one table

I thought this would be a simple query, but I'm obviously having problems with the query logic for outputting the data as desired. 我以为这将是一个简单的查询,但是很显然,查询逻辑无法按需输出数据。

I'd like to query two tables that have a key to relate the tables, and output the results from table1 (one row/record output) and then output the results from table2 (multiple rows/records output). 我想查询两个具有关联表的键的表,并从table1输出结果(一行/记录输出),然后从table2输出结果(多行/记录输出)。

I've tried UNIQUE, DISTINCT and subqueries to no avail. 我尝试了UNIQUE,DISTINCT和子查询无济于事。

Here is the table info. 这是表格信息。 and fields: 和字段:

tblfilm: tblfilm:

filmID (primary key) filmID(主键)

film 电影


film1 (record) 电影1(记录)


tblfilmimage: tblfilmimage:

imageID 图像标识

filmID (foreign key, ie primary key from tblfilm) filmID(外键,即tblfilm的主键)

image 图片


image1 (record) image1(记录)

image2 (record) image2(记录)


And here is the most recent SELECT statement I've tried: 这是我尝试过的最新SELECT语句:

SELECT film, image FROM tblfilm, tblfilmimage
WHERE tblfilm.filmID = 2

filmID = 2 is unique to one film in tblfilm, and = to multiple images in tblfilmimages, that show actors from that film. filmID = 2对于tblfilm中的一部电影是唯一的,并且=对于tblfilmimages中的多个图像是唯一的,这些图像显示了该电影中的演员。 Thus the overall goal is to be able to output multiple images for one film. 因此,总体目标是能够为一部电影输出多个图像。

Current Undesired Output: 当前不希望的输出:

film1 image1 

film1 image2

*I'm trying to eliminate the second film1 record from being output. *我正在尝试消除第二个film1记录的输出。

Desired Output: 所需输出:

film1  image1 image2

I'm pretty new to using mysql when querying multiple tables. 查询多个表时,我是使用mysql的新手。 And I'm sure there is an easy solution to this but I just can't seem to wrap my head around the logic. 而且我敢肯定有一个简单的解决方案,但是我似乎无法完全理解逻辑。 Thus any comments or suggestions appreciated. 因此,任何意见或建议表示赞赏。 Thank you. 谢谢。

UPDATE: I now realize my original question logic was flawed ... and my example not very clear. 更新:我现在意识到我原来的问题逻辑存在缺陷……而且我的例子不是很清楚。 The solution given by those that responded was correct for what I originally wrote. 回答者给出的解决方案对我最初写的是正确的。 Thank you. 谢谢。 I will try to clarify what I am trying to accomplish. 我将尝试澄清我要完成的工作。

TableA has a list of many films (example): TableA包含许多电影的列表(示例):

Wizard of OZ 绿野仙踪

Gone with the Wind ... etc. 乱世佳人...等

TableB has a list of images (example): TableB具有图像列表(示例):

Wizard of Oz Image1 绿野仙踪Image1

Wizard of Oz Image2 绿野仙踪Image2

Wizard of Oz Image3 ...etc. 绿野仙踪Image3 ...等

Gone with the Wind Image1 乱世佳人Image1

Gone with the Wind Image2 ... etc. 乱世佳人Image2 ...等

The desired output of the query would be: 查询的期望输出为:

Wizard of OZ 绿野仙踪

Wizard of Oz Image1 绿野仙踪Image1

Wizard of Oz Image2 绿野仙踪Image2

Wizard of Oz Image3 绿野仙踪Image3

Gone with the Wind 随风而逝

Gone with the Wind Image1 乱世佳人Image1

Gone with the Wind Image2 乱世佳人Image2

... and so on with hundreds of films and hundreds more images. ...等等,包括数百部电影和数百幅图像。

The solution to my original question showed me how to prevent multiple instances of the film name record within the output, but requires knowing and coding the ImageID for every image that is associated with a film. 我最初的问题的解决方案向我展示了如何在输出中防止电影名称记录的多个实例,但是需要知道和编码与电影相关的每个图像的ImageID。 Every film has at least one image to associate. 每部电影都有至少一个要关联的图像。 Most films have many and a varying number of images associated with the film. 大多数电影都有许多与电影相关的图像。 But the images associated with each film are unique to one film. 但是与每部电影相关的图像对于一部电影都是唯一的。 Thus I need a way to iterate through each film, find all the images associated with each film via the (filmID) which is a common field to each table (TableA - films & TableB - images). 因此,我需要一种方法来遍历每部影片,通过(filmID)查找与每部影片关联的所有图像,这是每个表格的共同字段(TableA-影片和TableB-图像)。 I think I will need a variable to store each unique film and a way to associate that film variable with a variable that stores all of the different images that have the same filmID as the film. 我想我将需要一个变量来存储每个独特的影片,并需要一种将影片变量与一个变量关联的方式,该变量存储与影片具有相同filmID的所有不同图像。 I will keep trying to figure out how to do that, but if anyone wants to point me in the right direction it would be appreciated. 我将继续尝试找出解决方法,但如果有人想指出正确的方向,我将不胜感激。 Thank you. 谢谢。

Use correlated subqueries within your queries by common column filmID to produce new columns : 在查询中通过公共列filmID使用correlated subqueries查询来生成新列:

SELECT film, 
       (SELECT image FROM tblfilmimage WHERE imageID=1 and filmID = f.filmID ) as image1, 
       (SELECT image FROM tblfilmimage WHERE imageID=2 and filmID = f.filmID ) as image2
  FROM tblfilm f
 WHERE f.filmID = 2;

or, use conditional aggregation directly as a better option: 或者,直接使用conditional aggregation作为更好的选择:

SELECT film, 
       MAX(CASE WHEN i.imageID=1 THEN i.image END ) as image1, 
       MAX(CASE WHEN i.imageID=2 THEN i.image END ) as image2
  FROM tblfilm f
  JOIN tblfilmimage i on i.filmID = f.filmID
 WHERE f.filmID = 2
 GROUP BY film;

Where the values for ImageID columns are just presumed by me. ImageID列的值仅由我推定。

In your case you had a CROSS JOIN by using comma-seperated tables. 在您的情况下,您使用逗号分隔的表进行了CROSS JOIN So, there were multiple rows generated without correlation through that. 因此,生成了多行而没有相关性。 This style is not suggested, and considered as old-syntax for SQL Select Statements. 不建议使用此样式,对于SQL Select语句,此样式被视为旧语法。 Whenever JOIN is needed, prefer using this JOIN syntax which is called ANSI-92 standard, easier to read, understand, and maintain. 每当需要JOIN时,都喜欢使用这种被称为ANSI-92标准的JOIN语法,它更易于阅读,理解和维护。

暂无
暂无

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

相关问题 使用mysql从同一数据库中的两个不同表中选择两列+在C#中使用查询输出 - select two columns from two different tables in the same db using mysql + use the output of query in C# 如何将来自两个表的结果合并到一个输出文件中? - How to join results from two tables in one output file? SQL查询是否根据来自其他两个表的数据返回一个表的结果? - SQL query to return results from one table based on data joined from two other tables? SELECT查询以从两个表中收集结果 - SELECT query to collect results from two tables PHP / SQL:仅使用一个查询,如果两个表中都有数据,则从两个表中选择行;否则,仅从一个表中进行SELECT - PHP/SQL: Using only one query, SELECT rows from two tables if data is in both tables, or just SELECT from one table if not 将两个表中的列合并为一个结果,然后在SELECT查询中使用 - Combine columns from two tables AS one result and THEN use in SELECT query 如何从一个限制为1个表的多个表中选择数据 - How to select data from multiple tables with limit 1 for one table 在一个查询中从两个表中选择多个数据 - Select multiple data from two tables in one query MySQL如何通过一个查询从不同的表中获得两个结果 - MySQL How to get two results with one query from different tables 如何进行选择查询以从一个表中选择数据并将结果提供给另一个选择 - How to make select query to select data from one table and results give to another select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM