MySQL - Select 来自多个表并显示多个字段
MySQL - Select from multiple tables and display multiple fields
我有一个带有 100 个 *_options
table 的 WordPress 多站点,即 WPM_101_options
到 WPM_201_options
。
我正在尝试查询选项名称 admin_email
、siteurl
和 blogname
。
此代码适用于 1 table:
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'blogname'
但是如何让它与多个 table 一起工作?
我尝试了 2 tables...
SELECT option_name, option_value
FROM wpm_104_options,wpm_105_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_105_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_105_options
WHERE option_name = 'blogname'
和
SELECT option_name, option_value
FROM wpm_104_options,wpm_101_options
WHERE option_name = 'admin_email'
GROUP BY option_name
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_101_options
WHERE option_name = 'siteurl'
GROUP BY option_name
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_101_options
WHERE option_name = 'blogname'
GROUP BY option_name
和
SELECT wpm_104_options.option_name as n104, wpm_104_options.option_value as v104, wpm_101_options.option_name as n101, wpm_101_options.option_value as v101
FROM wpm_104_options, wpm_101_options
WHERE option_name = 'admin_email'
...但是我得到一个错误:
Error Code: 1052. Column 'option_name' in field list is ambiguous 0.000 sec
错误表明您已尝试查询两个 table,每个都有一个 option_name
列,而数据库无法知道您指的是哪两个。
您在 FROM
子句中指定两个 table 所做的是一个 implicit join。如果您希望将两个 table 中的列加入您的结果中,您需要通过在它们前面加上 table 名称来指定您所指的列,例如
SELECT wpm_104_options.option_name, wpm_104_options.option_value, wpm_101_options.option_name, wpm_101_options.option_value
FROM wpm_104_options,wpm_101_options
WHERE wpm_104_options.option_name = 'admin_email'
GROUP BY wpm_104_options.option_name
然而,这通常只有在您还指定列应该以何种方式连接时才有意义,即在 WHERE
子句中定义它们的关系。否则,您只会从两个 table 中获得所有可能的行组合。
我猜你真正想要的是别的东西。我相信您只是想要显示一个 table 和两个 table 的查询的组合结果。这不是通过在 FROM
子句中使用两个 table 的隐式连接来实现的,而是像您已经使用的那样使用 UNION
或 UNION ALL
。例如
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'blogname'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'blogname'
您还可以通过在 WHERE
子句中使用 OR
:
指定不同的条件来大大缩短这个时间
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'admin_email'
OR option_name = 'siteurl'
OR option_name = 'blogname'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'admin_email'
OR option_name = 'siteurl'
OR option_name = 'blogname'
甚至
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name IN ('admin_email', 'siteurl', 'blogname')
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name IN ('admin_email', 'siteurl', 'blogname')
我有一个带有 100 个 *_options
table 的 WordPress 多站点,即 WPM_101_options
到 WPM_201_options
。
我正在尝试查询选项名称 admin_email
、siteurl
和 blogname
。
此代码适用于 1 table:
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'blogname'
但是如何让它与多个 table 一起工作? 我尝试了 2 tables...
SELECT option_name, option_value
FROM wpm_104_options,wpm_105_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_105_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_105_options
WHERE option_name = 'blogname'
和
SELECT option_name, option_value
FROM wpm_104_options,wpm_101_options
WHERE option_name = 'admin_email'
GROUP BY option_name
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_101_options
WHERE option_name = 'siteurl'
GROUP BY option_name
UNION
SELECT option_name, option_value
FROM wpm_104_options,wpm_101_options
WHERE option_name = 'blogname'
GROUP BY option_name
和
SELECT wpm_104_options.option_name as n104, wpm_104_options.option_value as v104, wpm_101_options.option_name as n101, wpm_101_options.option_value as v101
FROM wpm_104_options, wpm_101_options
WHERE option_name = 'admin_email'
...但是我得到一个错误:
Error Code: 1052. Column 'option_name' in field list is ambiguous 0.000 sec
错误表明您已尝试查询两个 table,每个都有一个 option_name
列,而数据库无法知道您指的是哪两个。
您在 FROM
子句中指定两个 table 所做的是一个 implicit join。如果您希望将两个 table 中的列加入您的结果中,您需要通过在它们前面加上 table 名称来指定您所指的列,例如
SELECT wpm_104_options.option_name, wpm_104_options.option_value, wpm_101_options.option_name, wpm_101_options.option_value
FROM wpm_104_options,wpm_101_options
WHERE wpm_104_options.option_name = 'admin_email'
GROUP BY wpm_104_options.option_name
然而,这通常只有在您还指定列应该以何种方式连接时才有意义,即在 WHERE
子句中定义它们的关系。否则,您只会从两个 table 中获得所有可能的行组合。
我猜你真正想要的是别的东西。我相信您只是想要显示一个 table 和两个 table 的查询的组合结果。这不是通过在 FROM
子句中使用两个 table 的隐式连接来实现的,而是像您已经使用的那样使用 UNION
或 UNION ALL
。例如
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'blogname'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'admin_email'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'siteurl'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'blogname'
您还可以通过在 WHERE
子句中使用 OR
:
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name = 'admin_email'
OR option_name = 'siteurl'
OR option_name = 'blogname'
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name = 'admin_email'
OR option_name = 'siteurl'
OR option_name = 'blogname'
甚至
SELECT option_name, option_value
FROM wpm_104_options
WHERE option_name IN ('admin_email', 'siteurl', 'blogname')
UNION
SELECT option_name, option_value
FROM wpm_101_options
WHERE option_name IN ('admin_email', 'siteurl', 'blogname')