简体   繁体   English

从2个不相关的表中选择语句

[英]select statement from 2 unrelated tables

There's 2 unconnected tables, with no common IDs 有2个未连接的表,没有公共ID

+---------------------+
| names               |
+------+--------------+
| name        | lastN |
+-------------+-------+
| Anthony     | monta |
| Ryo         | shizu |
+------+--------------+

+----------------------+
| nicknames            |
+------+---------------+
| nickname             |
+------+---------------+
| miso_hungry          |
+------+---------------+

I'm trying to run a select query on both tables and currently doing something like: 我试图在两个表上运行一个选择查询,目前正在做类似的事情:

SELECT names.name, nicknames.nickname
FROM names, nicknames
WHERE names.name="miso_hungry" OR nicknames.nickname="miso_hungry"

I'm getting back a weird results with repeating identical rows, which doesn't make sense. 通过重复相同的行,我得到了一个奇怪的结果,这没有任何意义。

For example if I search for miso_hungry with the query above it will return every row of "names" table for some reason and append the correct rows from the "nicknames" table.. 例如,如果我用上面的查询搜索miso_hungry,它将出于某种原因返回“名称”表的每一行,并从“昵称”表中追加正确的行。

Attaching a screenshot of the results 附上结果的屏幕截图

在此处输入图片说明

Above should show "NULL" under name column, since "miso_hungry" is not found in that column and I'm not sure why it prints every row of the "names" table also. 上面应该在名称列下显示“ NULL”,因为在该列中找不到“ miso_hungry”,而且我不确定为什么它也会打印“名称”表的每一行。

You can use UNION Clause 您可以使用UNION子句

  • Each SELECT statement within UNION must have the same number of columns UNION中的每个SELECT语句必须具有相同的列数
  • The columns must also have similar data types 这些列还必须具有相似的数据类型
  • The columns in each SELECT statement must also be in the same order 每个SELECT语句中的列也必须具有相同的顺序

So we need to made them satisfy above condition. 因此,我们需要使它们满足上述条件。 We can use Aliasing to do this. 我们可以使用别名来做到这一点。

SELECT name,(SELECT NULL) as nickname FROM names WHERE name = "miso_hungry"

UNION

SELECT (SELECT NULL) as name, nickname FROM nicknames WHERE nickname = "miso_hungry"

Edited 已编辑

If you want to get the match count from both table use query like below : 如果要从两个表中获取匹配计数,请使用如下查询:

SELECT SUM(count) as count FROM ( 
    SELECT count(*) as count FROM names WHERE name = "miso_hungry"
    UNION ALL
    SELECT count(*) as count FROM nicknames WHERE nickname = "miso_hungry"
) both_table 

The order of execution in your statement is from,where,select. 语句中的执行顺序是从哪里选择的。 With and implicit join you get a cartesian product which given 使用和隐式联接,您将得到给定的笛卡尔积

use sandbox;
create table n(name varchar(20));
create table nn(nickname varchar(20));

insert into n values('Antony'),('Ryo');
insert into nn values('miso');

results in 结果是

MariaDB [sandbox]> SELECT n.name, nn.nickname
    -> FROM n, nn;
+--------+----------+
| name   | nickname |
+--------+----------+
| Antony | miso     |
| Ryo    | miso     |
+--------+----------+
2 rows in set (0.00 sec)

The where clause is then applied - which yields the same result. 然后应用where子句-产生相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM