一行中多个表的值
我有以下 3 个表
Table1
ID | NAME
-----------
1 | X
2 | Y
3 | Z
Table2
ID | NAME
-----------
1 | A
2 | B
3 | C
Table3
ID | P (Boolean field) | Other cols
1 | True ...
2 | True....
1 | False
现在我需要对 table3 进行查询,该查询必须执行以下操作:
显示 table1 和 table2 的名称字段。 但我的问题是 如果 table3 上的字段 P 为 true,我希望它显示 table2 的字段名称,其中 table2.id = table3.id,但如果为 false,我需要它读取 table1 的名称字段的名称,其中 table1.id = table3.id。
显示结果的程序是一个桌面应用程序,我可以使用一些过程或其他东西来显示结果,但如果我有一个 SQL 查询来为我执行此操作,那就更好了。
I have the following 3 tables
Table1
ID | NAME
-----------
1 | X
2 | Y
3 | Z
Table2
ID | NAME
-----------
1 | A
2 | B
3 | C
Table3
ID | P (Boolean field) | Other cols
1 | True ...
2 | True....
1 | False
Now I need a query on table3 that has to do the following:
To display the name field of table1 and table2. But my problem is that
if field P on table3 is true I want it to display the name of table2's field where table2.id = table3.id but if it is false I need it to read the name of table1's name field where table1.id = table3.id.
The program which will display the results is a desktop application and i could do it with some procedure or something to display them, but would be nicer if I had a SQL query to do this for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个:
,或者这个:
在能够执行
HASH JOIN
的系统中(即Oracle
、SQL Server
、PostgreSQL
,但不是MySQL
),如果布尔值均匀分布,第二个更好,即有很多TRUE
和FALSE
的,并且如果table3
相当大。如果分布存在偏差,如果
table3
中的行数比table1
或table2
中的行数少得多,或者如果您正在使用MySQL
。更新:
如果大多数字段都为 false,则以下查询可能是最好的:
此处的子查询将仅用作后备,并且仅在罕见的
TRUE值。
更新 2:
我无法在 Firebird 上检查它,但在大多数系统上,上面查询中的
ORDER BY
语法都可以工作。 如果没有,请将查询包装到内联视图中:尽管这可能会影响性能(至少在
MySQL
中是这样)。This:
, or this:
In systems capable of doing
HASH JOIN
(that isOracle
,SQL Server
,PostgreSQL
, but notMySQL
), the second one is better if the boolean values are distributed evenly, i. e. there are lots of bothTRUE
's andFALSE
's, and iftable3
is quite large.The first one is better if there is a skew in distribution, if there are much fewer rows in
table3
then intable1
ortable2
, or if you are usingMySQL
.Update:
If the majority of fields are false, the following query will probably be the best:
Subquery here will only be used as a fallback, and will be executed only for the rare
TRUE
values.Update 2:
I can't check it on Firebird, but on most systems the
ORDER BY
syntax as in query above will work. If it does not, wrap the query into an inline view:, though it may hamper performance (at least in
MySQL
it does).您可以使用
you can use
可能不是一个选择,但有时架构重新设计可以解决很多问题。 任何执行每行函数的解决方案都应该非常仔细检查性能问题(实际上任何查询都应该持续监视性能问题,尤其是每行函数的性能问题)。
考虑将表 1 和表 2 组合在一起:
然后你的查询变得相对简单(而且几乎肯定快得令人眼花缭乱):
性能问题通常只出现在大型表、大型机大小的情况下,其中 500 万行被视为配置表:-)对于您正在处理的那种桌子来说,它可能不是必需的,但它是一个有用的工具。
Possibly not an option but sometimes a schema redesign will solve a lot of problems. Any solution that executes per-row functions should be examined very carefully for performance problems (actually any query should be continuously monitored for performance problems but especially per-row function ones).
Consider combining tables 1 and 2 together thus:
Then your query becomes a relatively simple (and almost certainly blindingly fast):
The performance issue usually only rears its head for large tables, mainframe sizes where 5 million rows is considered a configuration table :-) It may not be necessary for the sort of tables you're dealing with but it's a useful tool to have in the armory.
就像mysql中的这个查询一样简单。
It's as simple as this query in mysql.