来自2个连接表的联盟信息

时间:2018-04-18 10:18:27

标签: sql firebird

我的firebird数据库中有2个表

表格问题:

| qsn_id | qsn_text |
|--------|----------|
| 1      | Qsn1     |
| 2      | Qsn2     |

表格及答案:

| ans_id | ans_text | qsn_id |
|--------|----------|--------|
| 1      | Answer1  | 1      |
| 2      | Answer2  | 1      |
| 3      | Answer1  | 2      |
| 4      | Answer2  | 2      |

是否有任何查询可以获得这样的信息:

| Text    |
|---------|
| Qsn1    |
| Answer1 |
| Answer2 |
| Qsn2    |
| Answer1 |
| Answer2 |

2 个答案:

答案 0 :(得分:2)

您可以使用CarConfiguration x = xmlDocument.Deserialize<CarConfiguration>(); union all

order by

<强>输出

select text
from ((select q.qsn_text as text, q.qsn_id, 0 as ans_id
       from questions q
      ) union all
      (select a.ans_text, a.qsn_id, a.ans_id
       from answers a
      )
     ) qa
order by qsn_id, ans_id;

<强>演示

  

http://sqlfiddle.com/#!9/a742f7/3

答案 1 :(得分:0)

你也可以运行 EXECUTE BLOCK

$ cat foo.c
#include <sys/types.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
    int *foo = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    *foo = 42;
    printf("%d\n", *foo);
    mmap(foo, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
    printf("%d\n", *foo);
    return 0;
}
$ cc -o foo foo.c && ./foo
42
0
$

您可以将EXECUTE BLOCK RETURNS (Text VARCHAR(100)) AS DECLARE VARIABLE qsn_id int; BEGIN FOR SELECT question.qsn_id, question.qsn_text FROM question ORDER BY question.qsn_id ASC INTO :qsn_id, :Text DO BEGIN SUSPEND; FOR SELECT answers.ans_text FROM answers WHERE answers.qsn_id = :qsn_id ORDER BY answers.ans_id ASC INTO :Text DO BEGIN SUSPEND; END END END 替换为EXECUTE BLOCK,将执行阻止修改为已存储的程序

如果您创建了 STORED PROCEDURE ,则必须调用SQL

CREATE OR ALTER PROCEDURE ProcedureName

输出是:

SELECT *
FROM procedurename