简体   繁体   English

使用 where 条件连接 2 个表,但显示 table2 中的第一行

[英]JOIN 2 tables with where condition, but show first row from table2

I have 2 tables.我有2张桌子。 One is movie table, where all movie data is stored and another one is relation table, where I can see which movie is attached to which category.一个是电影表,其中存储了所有电影数据,另一个是关系表,我可以在其中查看哪个电影附加到哪个类别。 If I want to show movies from category 6 I join them like this:如果我想放映第 6 类电影,我会像这样加入它们:

SELECT t1.title, t2.category FROM movies t1
JOIN movie_categories t2 ON t1.movieid = t2.movieid
WHERE t2.category = 6

And it works just fine.它工作得很好。 However, movie SEF url are generated based on the first row from table2 (actually it should connect to table3, a category table, where i will find sef alias name, but for now I dont need that, I just want to know how to get first category).但是,电影 SEF url 是基于 table2 的第一行生成的(实际上它应该连接到 table3,一个类别表,在那里我会找到 sef 别名,但现在我不需要那个,我只想知道如何获取第一类)。 So lets say, movie have 3 categories, which will be looking like this:可以说,电影有 3 个类别,如下所示:

Table 2 [relations table]
id movieid category
 1   1      3   
 2   1      6   
 3   1      2   

Table 1 [movie table]
movieid
   1      
   2     

The query I showed before returns results like this:我之前展示的查询返回这样的结果:

title category
bla1      6
bla2      6

However to create sef url, I need to get the first category id from relations table which is 3 in this case.但是,要创建 sef url,我需要从关系表中获取第一个类别 id,在这种情况下为 3。

I'd like to know how to construct query to achieve the result I described.我想知道如何构造查询来实现我描述的结果。

ASSUMPTION: by MIN you mean the lowest ID in movie_Categories and then display it's category value.假设:MIN 是指movie_Categories 中最低的ID,然后显示它的类别值。

  1. Generate a set of data which is the lowest ID for each movie (subquery)生成一组数据,它是每部电影的最低 ID(子查询)
  2. Join it back to the movie list将其重新加入电影列表
  3. Then join your movie categories back to the generated set with the min value然后将您的电影类别加入到具有最小值的生成集
  4. Join the movie categories again back to the base movie set to get the limiting set for category.再次将电影类别加入到基本电影集以获得类别的限制集。
  5. Then limit the data base the category desired.然后将数据库限制为所需的类别。

. .

drop table movie;
drop table movie_categories;
create table movie (movieid int, Title varchar(10));
create table movie_categories (Id int, movieID int, category int);

insert into movie values (1, 'The');
insert into movie values (2, 'End');
insert into movie_categories values (1,1,3);
insert into movie_categories values (2,1,6);
insert into movie_categories values (3,1,2);

--To aid in understanding you may want to select * and see why this works and remove the where clause.
--Basically the two joins to Movie_Categories is once for the LOWEST ID and once again to get the limiting category.
SELECT M.Title, MC1.Category
FROM Movie M
INNER JOIN (SELECT min(ID) ID, MovieID
            from movie_categories mc
            GROUP BY MovieID) L
 on L.MovieID = M.MovieID
INNER JOIN Movie_Categories MC1 
 on  L.MovieID = MC1.MovieID
INNER JOIN Movie_Categories MC2
 on l.movieid = mc2.movieid
 and L.ID = MC1.ID
where mc2.category = 6

Results in:结果是:

title category
The     3

Note: the 2nd title isn't listed because there are no records in movie_category which match a category of 6.注意:第二个标题没有列出,因为在 movie_category 中没有匹配类别 6 的记录。

暂无
暂无

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

相关问题 如何在表2所在的表2行中满足条件的情况下将表1左联接 - How to LEFT JOIN table1 ON table2 WHERE table2 row fulfills certain conditions Mysql left join table2 where field from table3 - Mysql left join table2 where field from table3 如何联接条件“在哪里”与与第一个表相关联的第三个表相关的两个表 - how to join two tables where the condition 'Where' is related to a third table that is associated with the first table 仅从具有where条件的联接表中选择第一条记录 - Selecting first record only from join table with a where condition 如何将表1中的行与表2中的数据连接,其中表2中的所有数据都具有表1中的行ID - How to connect row from table1 with data in table2 where all data in table2 has id of row from table1 连接两个表,条件是第一个表 - Join two tables with condition on the first table 使用内连接查询不显示表 1 中的 id 不在表 2 中的行 - Query doesn't show table1 row which id is not in table2 using inner join SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause MYSQL连接,仅使用OR从连接条件所在的位置返回第一个匹配行 - MYSQL join, return first matching row only from where join condition using OR 从三个表中选择数据,其中按行顺序显示第一个表中的所有记录,并水平显示另一个表的所有重复项 - Select data from three tables where show all records in first table row wise and all duplicates of the other horizontally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM