English 中文(简体)
将多克谢克尔并入一份声明
原标题:Combining multiple SQL Queries into one statement
  • 时间:2012-01-14 23:11:38
  •  标签:
  • sql

我正在寻找一种办法,把多处问询合并起来,以避免需要保持数据库。 我有三张表格,一张附有俱乐部详情的表格,一张与俱乐部设施连接的表格,第三张合并。

表格的简单版本:

图1

id, name, city
1, curby, London
2, zoo, Birmingham

图2

id, facility
1, bar
2, function suite
3, VIP room

t3

id, club_id, facility_id
1, 2, 1
2, 2, 3
3, 1, 2
4, 2, 2

在上述所有各栏中,第一栏(id)仅是独一无二的识别标志,而我是用漫画显示每个领域分开的地方。 我愿从数据库中获取的东西是:

1, curby, London, function suite
2, zoo, Birmingham, bar, VIP room, function suite

如果有任何想法,请分享:

最佳回答

页: 1 JOINs。 你们没有说你们重新使用过什么,但这正是我在Oracle中使用的:

SELECT t3.id, t2.name, t2.city, t3.facility
  FROM t3
 INNER JOIN t2 ON t2.id = t3.facility_id
 INNER JOIN t1 ON t1.id = t3.club_id
问题回答

OK,即1号俱乐部,2号是证据清单,3号是向俱乐部分配设施?

您可以利用JOIN(INNER或OUTER)的辛塔,一度从多个表格获取数据。 当母体表(t1)有数据,但儿童表中没有任何数据(t2)。 当你只需要两个表格的数据记录时,就使用INNER JOIN。

你们还必须包括一个“国家”条款,表明两个表格是如何合并的:

SELECT t1.id as club_id, t1.name as club_name, t2.facility as facility_name
FROM t1
INNER JOIN t2 ON
    t2.club_id=t1.id
INNER JOIN t3 ON
    t3.id=f2.facility_id

而且,如果我是的话,我实际上会把表格的名称改为更好地代表表格中的数据。

考虑重新命名:

  • t1 to Clubs and its id to club_id;
  • t2 to Facilities and its id to facility_id;
  • t3 to ClubFacilities and dropping its needless id column.

然后,请你写

SELECT Clubs.*, facility
  FROM Clubs 
       NATURAL JOIN Facilities 
       NATURAL JOIN ClubFacilities;




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

热门标签