好的我有一个135000行的临时表,我试图将这个临时表中的一些值插入到其他表中。
这是我正在使用的架构
tvtemptable
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| PROGTITLE | text | YES | | NULL | |
| SUBTITLE | text | YES | | NULL | |
| EPISODE | text | YES | | NULL | |
| YR | year(4) | YES | | NULL | |
| DIRECTOR | text | YES | | NULL | |
| PERFORMERS | text | YES | | NULL | |
| PREMIERE | tinyint(1) | YES | | NULL | |
| FILM | tinyint(1) | YES | | NULL | |
| RPEAT | tinyint(1) | YES | | NULL | |
| SUBTITLES | tinyint(1) | YES | | NULL | |
| WIDESCREEN | tinyint(1) | YES | | NULL | |
| NEWSERIES | tinyint(1) | YES | | NULL | |
| DEAFSIGNED | tinyint(1) | YES | | NULL | |
| BNW | tinyint(1) | YES | | NULL | |
| STARRATING | tinyint(4) | YES | | NULL | |
| CERTIFICATE | varchar(5) | YES | | NULL | |
| GENRE | varchar(50) | YES | | NULL | |
| DESCRIPTION | text | YES | | NULL | |
| CHOICE | tinyint(1) | YES | | NULL | |
| PROGDATE | date | YES | | NULL | |
| STARTIME | time | YES | | NULL | |
| ENDTIME | time | YES | | NULL | |
| DURATION | int(11) | YES | | NULL | |
| CHANNELID | int(11) | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+
信道
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| CHANNELID | int(11) | NO | PRI | NULL | auto_increment |
| CHANNELNAME | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
类型
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| GENREID | int(11) | NO | PRI | NULL | auto_increment |
| GENRENAME | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
程序
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| PROGRAMMEID | int(11) | NO | PRI | NULL | auto_increment |
| GENREID | int(11) | NO | MUL | NULL | |
| PROGTITLE | text | YES | | NULL | |
| YR | year(4) | YES | | NULL | |
| DIRECTOR | text | YES | | NULL | |
| PERFORMERS | text | YES | | NULL | |
| FILM | tinyint(1) | YES | | NULL | |
| WIDESCREEN | tinyint(1) | YES | | NULL | |
| BNW | tinyint(1) | YES | | NULL | |
| CERTIFICATE | varchar(5) | YES | | NULL | |
| DESCRIPTION | text | YES | | NULL | |
+-------------+------------+------+-----+---------+----------------+
插曲
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| EPISODEID | int(11) | NO | PRI | NULL | auto_increment |
| PROGRAMMEID | int(11) | NO | MUL | NULL | |
| SUBTITLE | text | YES | | NULL | |
| EPISODE | text | YES | | NULL | |
| DIRECTOR | text | YES | | NULL | |
| PERFORMERS | text | YES | | NULL | |
| DESCRIPTION | text | YES | | NULL | |
+-------------+---------+------+-----+---------+----------------+
channelprogramme
+--------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------+------+-----+---------+----------------+
| CHANNELPROGRAMMEID | int(11) | NO | PRI | NULL | auto_increment |
| CHANNELID | int(11) | NO | MUL | NULL | |
| PROGRAMMEID | int(11) | NO | MUL | NULL | |
| EPISODEID | int(11) | NO | MUL | NULL | |
| RPEAT | tinyint(1) | YES | | NULL | |
| NEWSERIES | tinyint(1) | YES | | NULL | |
| PREMIERE | tinyint(1) | YES | | NULL | |
| CHOICE | tinyint(1) | YES | | NULL | |
| SUBTITLES | tinyint(1) | YES | | NULL | |
| DEAFSIGNED | tinyint(1) | YES | | NULL | |
| STARRATING | tinyint(4) | YES | | NULL | |
| PROGDATE | date | YES | | NULL | |
| STARTTIME | time | YES | | NULL | |
| ENDTIME | time | YES | | NULL | |
| DURATION | tinyint(4) | YES | | NULL | |
+--------------------+------------+------+-----+---------+----------------+
所以有一点背景,数据库是电视列表数据库,channelprogramme表包含每个频道上每个节目的所有条目,节目表包含每个节目的唯一条目,剧集包含每集的唯一条目有剧集的节目
我认为我的困惑是我想从临时表中选择不同的progtitles来填充程序表,但是我想要一些其他的信息。
例如,如果我做了
select distinct(progtitle) from tvtemptable;
它只返回progtitle列的值,而我实际想要的是progtitle加上其他一些东西。
所以,如果我尝试选择更多这样的信息
mysql> select distinct progtitle, yr, director, film from tvtemptable
limit 30;
+-----------------------------------+------+---------------------+------+
| progtitle | yr | director | film |
+-----------------------------------+------+---------------------+------+
| Teleshopping | 2000 | | 0 |
| Cinemania | 2000 | | 0 |
| Whose Line Is It Anyway? | 2000 | | 0 |
| Just Desserts | 2004 | Kevin Connor | 1 |
| Law & Order | 2000 | Matthew Penn | 0 |
| Jane Doe: Yes, I Remember it Well | 2006 | Armand Mastroianni | 0 |
| CSI: NY | 2000 | David Jackson | 0 |
| CSI: Crime Scene Investigation | 2000 | Kenneth Fink | 0 |
| NCIS | 2000 | Colin Bucksey | 0 |
| CSI: Miami | 2000 | | 0 |
| Enter the Dragon | 1973 | Robert Clouse | 1 |
| Close | 2000 | | 0 |
| My Son Is Innocent | 1996 | Larry Elikann | 1 |
| Law & Order | 2000 | Christopher Misiano | 0 |
| Murder 101 | 2006 | Christian I Nyby II | 1 |
| CSI: NY | 2000 | Christine Moore | 0 |
| CSI: Crime Scene Investigation | 2000 | Bill Eagles | 0 |
| Rush Hour | 1998 | Brett Ratner | 1 |
| Dark Blue | 2000 | Jeffrey Hunt | 0 |
| CSI: Crime Scene Investigation | 2000 | Richard J Lewis | 0 |
| Ordinary Miracles | 2005 | Michael Switzer | 1 |
| Law & Order | 2000 | Jace Alexander | 0 |
| Wounded Heart | 1995 | Vic Sarin | 1 |
| CSI: NY | 2000 | Jonathan Glassner | 0 |
| Dark Blue | 2000 | Nathan Hope | 0 |
| Blade: The Series | 2000 | Michael Robison | 0 |
| K-Ville | 2000 | Kevin Dowling | 0 |
| Law & Order | 2000 | Jim Ellis | 0 |
| Reasons of the Heart | 1996 | Rick Jacobson | 1 |
| CSI: NY | 2000 | Anthony Hemingway | 0 |
+-----------------------------------+------+---------------------+------+
progtitle列中会有一些重复项。每个新导演都不会重复这些节目,某些节目上面有不同的导演,因为这些节目有剧集。
现在我已经设法让选择正常工作如果我只需要一个额外的列例如
select distinct
t1.progtitle,
(select
t2.director
from
tvtemptable t2
where
t1.progtitle = t2.progtitle
limit 1) as "Director"
from
tvtemptable t1 limit 10;
+-----------------------------------+--------------------+
| progtitle | Director |
+-----------------------------------+--------------------+
| Teleshopping | |
| Cinemania | |
| Whose Line Is It Anyway? | |
| Just Desserts | Kevin Connor |
| Law & Order | Matthew Penn |
| Jane Doe: Yes, I Remember it Well | Armand Mastroianni |
| CSI: NY | David Jackson |
| CSI: Crime Scene Investigation | Kenneth Fink |
| NCIS | Colin Bucksey |
| CSI: Miami | |
+-----------------------------------+--------------------+
显然这会变得混乱如果我想选择多个额外的列
那么最好的方法是什么?
这是我用于从临时表中填充程序表的插入命令,目前它与我在上面给出的选择示例中的重复项有相同的问题。
INSERT INTO PROGRAMME (
PROGTITLE, GENREID, YR, DIRECTOR,
PERFORMERS, FILM, WIDESCREEN, BNW,
CERTIFICATE, DESCRIPTION)
SELECT DISTINCT
T.PROGTITLE, G.GENREID, T.YR, T.DIRECTOR,
T.PERFORMERS, T.FILM, T.WIDESCREEN, T.BNW,
T.CERTIFICATE, T.DESCRIPTION
FROM
TVTEMPTABLE T
INNER JOIN GENRE G ON G.GENRENAME=T.GENRE
LEFT JOIN PROGRAMME P ON P.PROGTITLE=T.PROGTITLE
WHERE
P.PROGTITLE IS NULL
答案 0 :(得分:0)
这没有意义。一行如何部分区分并且部分不同?将所选结果视为一个表 - 如果PROGTITLE
有一个值,其余列有多个值,您将如何表示数据?
答案 1 :(得分:0)
这不能回答你的问题,但可能会回答那些在谷歌上搜索的人:
在Postgresql中,您可以使用SELECT DISTINCT ON执行此操作,例如:
SELECT DISTINCT ON (p.progtitle) p.* FROM progtitle p;
我不知道MySQL的任何等价物。
答案 2 :(得分:0)
Insert into xx (a,b)
Select a.ab, b.bb from (
Select distinct ab from a) a, (select distinct bb from b) b
答案 3 :(得分:0)
我是否正确地认为您的TVTEMPTABLE
是来自某个地方的表格连接的结果?
如果是这种情况,如果您首先将该临时表拆分为多个与目标表格格式相同的临时表,我认为您的整体工作会更轻松。
例如(我不知道您的数据,所以我猜):
-- All genres (matches your existing genres table)
create table genres_temp as
select distinct genre
from tvtemptable;
-- All programmes (matches your existing programme table)
create table programmes_temp as
select distinct all_the_programme_columns
from tvtemptable;
-- Contains the many-to-many relationship between genres and programmes
create table programme_genre_temp as
select distinct genre, progtitle
from tvtemptable;
上面没有回答你的问题,但它可能会给你一些消除问题的想法,似乎是每个某事重复了程序信息。你需要找出它是什么。
<强>更新强> 那么每个导演都会重复一次每个节目? 然后我的上述方法更有意义:首先提取实体和关系。
更新2
嗯,当导演与剧集有关时,你强迫导演进入节目吗?这似乎是一个建模错误。无论如何,如果您不关心自己选择的导演,可以在progtitle
上进行分组,并在所有其他专栏周围使用MAX()
。这将为您提供不同的progtitles和其余列的值之一。
SELECT T.PROGTITLE, max(G.GENREID), max(T.YR), max(T.DIRECTOR), etc
FROM TVTEMPTABLE T
INNER JOIN GENRE G ON G.GENRENAME=T.GENRE
LEFT JOIN PROGRAMME P ON P.PROGTITLE=T.PROGTITLE
WHERE P.PROGTITLE IS NULL
group by T.PROGTITLE;
答案 4 :(得分:0)
我正在读你想要的是一个不同程序的列表,但在上下文的其他列中有一些相关数据?你可以用行号来做到这一点。
select * from
(select *,
row_number() over (partition by progtitle order by year desc) N
from tvtempta) t
where t.N = 1
更好的是,您可以使用order by
子句指定选择所需行的最新,第一或其他首选方式。这是T-SQL语法,我认为相同的语法应该在Oracle中起作用(如果不是,它在Oracle中肯定是可行的)。不幸的是,我不知道你是否可以在MySQL中做到这一点。