English 中文(简体)
比较两个表格, 并按日抽取缺失元素
原标题:Compare two tables and extract missing element per date

大家好,我对一个 SQL 查询有问题,我找不到解决办法。

在我的数据库里,我有两个表格, Mytable1 和 Mytable2, Mytable1 包含 idCalendar 列、 idPlayer 、 datePlaying 和 Mytable2 包含 idPlayer 和 namePlayer 。

我想在Mytable1中提取每个日期的未玩耍的玩家的名字。

For example Mytable1 (contains player and playing date):

idCalendar|idPlayer|datePlaying

1         |     1  | 05/01/2012
2         |     2  | 06/01/2012
3         |     3  | 08/01/2012
4         |     2  | 05/01/2012

Myable2( 包含所有玩家名称) :

idPlayer|namePlayer

1  | P1
2  | P2
3  | P3
4  | P4

我想创建一个显示以下结果的视图 :

ViewResult( 尚未按日期播放的玩家) :

datePlaying | playerName

05/01/2012  | P2
05/01/2012  | P3
06/01/2012  | P1
06/01/2012  | P3
08/01/2012  | P1
08/01/2012  | P2

As you can see I need result with this information: - in 05/01/2012 P2 and P3 haven t played. - in 06/01/2012 P1 and P3 haven t played. - in 08/01/2012 P1 and P2 haven t played.

我希望我说得够清楚了 事先谢谢你

最佳回答

也许像这样简单的东西 会有效。

select
    d.datePlaying, p.namePlayer
from
    table2 p
cross join 
    (select distinct datePlaying from table1) d
left join
    table1 t on t.idPlayer = p.idPlayer and t.datePlaying = d.datePlaying
where
    t.idPlayer is null

作为侧边注意, 如果您有一个非玩家玩耍的日期, 会发生什么? 您不会在表2中有一个条目 。 您是否关心或者应该有一个第三个表格来代表任何一个玩家应该玩过的日期 。 此日期表格将用来替换我的别名 d 交叉组合的表格 。

问题回答

我这台电脑上没有DB手法 所以我现在不能测试这个—— 我会继续研究它。

SELECT     DISTINCT a.datePlaying, b.playerName
FROM       Mytable1 a
INNER JOIN Mytable2 b
        ON a.idPlayer = b.idPlayer
WHERE      NOT EXISTS (
           SELECT idPlayer
           FROM   Mytable1 a2
           WHERE  a.datePlaying = a2.datePlaying)
ORDER BY   a.datePlaying




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签