2

说我有以下内容:

CREATE TABLE newtable AS (
SELECT @rownum:=@rownum+1 as rownum, name, age FROM (
    SELECT name, age FROM clubAmembers
    UNION
    SELECT name, age FROM clubBmembers
)
) AS atable

我怎样才能做到这一点,以便我可以在 SELECT 联合之前“在表的开头插入一个新行”,以便它以:

rownum | name| age
1 | "Jordan" | 6  <-- This is an arbitrarily inserted record with name="Jordan" age="6" that is not a part of any of the clubAmembers or clubBmembers table.

表的其余部分(第 2 行及以后)将包含由 clubAmembers 然后是 clubBmembers 的联合的实际结果。

基本上我正在寻找: CREATE TABLE INSERT a row "Jordan" | 6 使用 union 执行 select,使第一行之后的行以“rownum=2”开头,所有来自 clubAmembers 的数据等。

如何最好地做到这一点?

4

4 回答 4

1

我不确定我是否做对了。但是你为什么不像这样添加另一个联合:

CREATE TABLE newtable AS (
SELECT @rownum:=@rownum+1 as rownum, name, age FROM (
    SELECT 1, "Jordan", 6
    UNION ALL
    SELECT name, age FROM clubAmembers
    UNION ALL
    SELECT name, age FROM clubBmembers
)
) AS atable
于 2013-02-06T05:40:56.893 回答
1

“在表的开头”对关系数据库没有真正意义,因为在您使用ORDER BY子句之前不能保证返回的顺序结果,此时磁盘上的顺序无论如何都成为一个有争议的问题。

在您的情况下,由于您想保证结果子句中的顺序(因此 ordering @rownum,您将不得不使用ORDER BY。例如:

CREATE TABLE newtable AS (
    SELECT @rownum:=@rownum+1 as rownum, name, age 
    FROM (
        SELECT 'Jordan' AS name, 6 AS age, 0 AS ord
        UNION
        SELECT name, age, 1 AS ord FROM clubAmembers
        UNION
        SELECT name, age, 1 AS ord FROM clubBmembers
        ORDER BY ord
    )
) AS atable

请注意,这绝不保证行 inclubAmembers将低于rownum行 in clubBmembers。如果要保证clubAmembers具有 lower rownum,同时保持UNION(versus UNION ALL) 的语义,可以使用以下内容:

CREATE TABLE newtable AS (
    SELECT @rownum:=@rownum+1 as rownum, name, age 
    FROM (
        SELECT 'Jordan' AS name, 6 AS age, 0 AS ord
        UNION ALL
        SELECT name, age, 1 AS ord FROM clubAmembers
        UNION ALL
        SELECT name, age, 2 AS ord FROM clubBmembers AS b
        WHERE NOT EXISTS(SELECT 1 FROM clubAmembers AS a 
                         WHERE a.name = b.name AND a.age = b.age)
        ORDER BY ord
    )
) AS atable

请注意,如果 { name, age} 可以在clubXmembers表中重复,则需要添加DISTINCT

...
SELECT DISTINCT name, age, 1 AS ord FROM clubAmembers
UNION ALL
...

根据评论中的要求,如果你有一张clubCmembers桌子,你会这样做:

CREATE TABLE newtable AS (
    SELECT @rownum:=@rownum+1 as rownum, name, age 
    FROM (
        SELECT 'Jordan' AS name, 6 AS age, 0 AS ord
        UNION ALL
        SELECT name, age, 1 AS ord FROM clubAmembers
        UNION ALL
        SELECT name, age, 2 AS ord FROM clubBmembers AS b
        WHERE NOT EXISTS(SELECT 1 FROM clubAmembers AS a 
                         WHERE a.name = b.name AND a.age = b.age)
        SELECT name, age, 3 AS ord FROM clubCmembers AS c
        WHERE NOT EXISTS(SELECT 1 FROM clubAmembers AS a 
                         WHERE a.name = c.name AND a.age = c.age)
        AND NOT EXISTS(SELECT 1 FROM clubBmembers AS b
                       WHERE b.name = c.name AND b.age = c.age)
        ORDER BY ord
    )
) AS atable
于 2013-02-06T05:42:08.043 回答
0

您可以将 create table statmenet 与 insert statmenet 分开:

  1. 创建表(你必须知道哪些列在那里)
  2. 插入您的第一条记录(INSERT INTO .... Values(...)
  3. 使用您的语句,但使用 insert into 而不是创建表,例如:(INSERT INTO YourNewTable.... Values(YourSubQuery)列的 Nr 和 tye 必须与您的子查询匹配)
于 2013-02-06T05:41:46.873 回答
0

我相信这应该这样做:

CREATE TABLE newtable AS (
  SELECT (@rownum:=IFNULL(@rownum,0)+1)+1 as rownum, name, age FROM (
    SELECT name, age FROM clubAmembers
    UNION
    SELECT name, age FROM clubBmembers
  ) AS s
  UNION ALL
  SELECT 1, 'Jordan', 6
) AS atable

SQL Fiddle 演示:http ://sqlfiddle.com/#!2/ab825/6

于 2013-02-06T09:23:59.623 回答