English 中文(简体)
结构逻辑:两个表格(按顺序排列)
原标题:SQL logic: two tables (group by?)
  • 时间:2009-11-25 00:01:50
  •  标签:
  • sql

我有两张桌子,一张电影桌(mt,小学桌)和一个表格,列出这些电影的可用地点(第二级桌)。 每一记录都有多个记录。 我需要一个将并入记录的问题,并请允许我在两个表格上提出问题。 我目前利用一个内心协力这样做:

SELECT * FROM
(
    SELECT ROW_NUMBER() OVER(ORDER BY " + orderField + @") AS RowNum,
           mt.ID AS mt_ID,
           mt.title AS mt_title,
           [...]
           st.ID AS st_ID,
           st.title AS st_title,
           [...]
    FROM mt AS mt 
    st AS st
    INNER JOIN sttable AS st on mt.ID =st.ID
    WHERE st.title=@variable <> 0 AND mt.title = @variable
)    
    as DerivedTableName
    WHERE RowNum between 
    ((@pageIndex - 1) * @pageSize + 1) and @pageIndex*@pageSize

问题在于,我需要能够使用罗恩图姆和英得特网页,每部电影的查询记录都不止一次(例如,如果某一电影有8个记录,8个而不是1个记录被退回)。 我尝试使用小组,但问题在于,它将不允许我就次表(st)中的领域提问。

对适当逻辑的任何帮助都会受到高度赞赏。

最佳回答

这里的解决办法是<代码> 下表按列出的组别,因此每部电影只有一行,并从中计算ROW_NUMBER(

然后,在外壳中加入到<代码>t的衍生表。 每个电影标题仍有多个浏览器,但RowNum将重复使用,以便你能够正确过滤@pageIndex

SELECT * FROM
(
    SELECT ROW_NUMBER() OVER(ORDER BY " + orderField + @") AS RowNum,
           mt.ID AS mt_ID,
           mt.title AS mt_title,
           [...]
    FROM mt AS mt 
    INNER JOIN sttable AS st ON mt.ID =st.ID
    WHERE mt.title = @variable
    GROUP BY mt.ID
) mt1
INNER JOIN sttable AS st1 ON (mt1.ID = st1.ID)
WHERE mt1.RowNum BETWEEN 
    ((@pageIndex - 1) * @pageSize + 1) AND @pageIndex*@pageSize;

您必须放弃显示代码中的外在查询结果,每当价值<代码>RowNum发生变化时,就开始新一轮产出。 这是一种非常明显的技术。

如果你想要做像MySQL sGROUP_CONCAT(功能一样的话,这在Microsoftkou服务器(我假设你重新使用Microsoft)。

See blogs such as 缩略语

PS:根据你的口头描述,你的样本Q. 确实是明智的,因此,我最好写出一些可以理解的内容。 它没有与你的计划相匹配的保障。


评论:我不认为你需要按在座标的<条码>的任何栏目排列。 我不打算在表层的选定名单中列载st 。 表中加入的<代码>st的唯一理由是,将mt的行文限制在st的行文上。 但<代码> 单位:百万 ID 确保每行mt的浏览量只有一行。 (实际上,由于这是SQ服务器,而不是MySQL,你需要指定所有<代码>mt。) www.un.org/chinese/sc/presidency.asp 按条款分列的组别。


您的第二点评论:

我想首先展示有最近增加的对应记录的小行。

如果你使用分组职能,你可以增加组别的其他栏目。 例如,最新<代码>每个组<代码>m/code>的<<>t>t/code>为import(st.date_plus),且您可在该次表中添加这一栏。

但是,在分局中不使用ORDER。 很少有任何理由进行分类,因为通过使用分局可能会改变订单,导致联合调查网络或其他业务使用分局。

页: 1

SELECT * FROM
(
    SELECT ROW_NUMBER() OVER(ORDER BY " + orderField + @") AS RowNum,
           mt.ID AS mt_ID,
           mt.title AS mt_title,
           [...] -- other mt.* columns
           MAX(st.date_added) AS latest_date_added
    FROM mt AS mt 
    INNER JOIN sttable AS st ON mt.ID =st.ID
    WHERE mt.title = @variable
    GROUP BY mt.ID, -- other mt.* columns
) mt1
INNER JOIN sttable AS st1 ON (mt1.ID = st1.ID)
WHERE mt1.RowNum BETWEEN 
    ((@pageIndex - 1) * @pageSize + 1) AND @pageIndex*@pageSize
ORDER BY mt1.latest_date_added DESC, st1.date_added DESC;
问题回答

我不认为你需要这样做:

FROM mt AS mt 
  st AS st

为此:

INNER JOIN sttable AS st on mt.ID = st.ID




相关问题
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: ...