在MySQL中联接多个不相关的表 [英] Joining multiple unrelated tables in MySQL

查看:143
本文介绍了在MySQL中联接多个不相关的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个站点,该站点在许多表中存储<select>选项,并根据单个页面提取所有相关选项.此刻,我结束了这样的查询:SELECT foo FROM foo_tbl;SELECT bar FROM bar_tbl;etc.这确实不是一个坏问题,但是我必须逐个遍历每个选择结果.

I have a site that stores <select> options in a number of tables, and extracts all of the relevant ones depending on the individual page. At the moment, I wind up with a query like this: SELECT foo FROM foo_tbl;SELECT bar FROM bar_tbl;etc. It's not really a bad problem, but I have to iterate over each select result individually.

我想将它们全部提取到一个网格中,然后做类似的事情

I'd like to extract all of them into a single grid, then do something like

if $row['foo'] != NULL { add to the foo options }
if $row['bar'] != NULL { add to the bar options }
etc

如果我使用类似SELECT DISTINCT f.foo, b.bar FROM foo_tbl AS f, bar_tbl AS b的查询,则会结束所有可能的行组合(第一个foo第一栏,第二个foo第一栏,第二个foo第一栏,第二个foo第二栏等).

If I use a query like SELECT DISTINCT f.foo, b.bar FROM foo_tbl AS f, bar_tbl AS b, I wind up with every possible combination of rows (first foo first bar, first foo second bar, second foo first bar, second foo second bar, etc etc).

是否有一种选择的方法,在一个列中每个元素只有一个实例,并用空值填充列中的其余行?

Is there a way to do a select like that and having only once instance of each element in a column, and filling the rest of the rows in the column with nulls?

推荐答案

您可以执行以下操作: 假设表foo有a,b,c列,而表栏有d,e,f列

You could do something like the following: Assuming table foo has columns a,b,c and table bar has columns d,e,f

Select 'isFOO', a, b, c, null, null, null from foo
Union All
Select 'isBAR',null, null, null, d, e, f from bar

这篇关于在MySQL中联接多个不相关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆