简体   繁体   English

用什么加入?

[英]What join to use?

I have 2 different tables, which have just one field with same name ('username').我有 2 个不同的表,它们只有一个具有相同名称('用户名')的字段。 They're not related each other.他们彼此没有关系。 I need with just one query to select all the rows among them both which have this field equal to a given value.我只需要对 select 进行一次查询,其中的所有行都具有该字段等于给定值。

I came up with this, which is off course wrong... SELECT * FROM user AS a FULL JOIN future_user AS b WHERE a.username=x OR b.username=x我想出了这个,这当然是错误的... SELECT * FROM user AS a FULL JOIN future_user AS b WHERE a.username=x OR b.username=x

These the tables I'm talking about:这些我正在谈论的表格:

CREATE TABLE user
(
uid      mediumint(6) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,

username varchar(15) NOT NULL,
password varchar(15) BINARY NOT NULL,
mail     varchar(50) NOT NULL,

name     varchar(50) NOT NULL,
surname  varchar(50) NOT NULL,
birth    char(10) NOT NULL,
sex      tinyint(1) unsigned NOT NULL default 1,

address  varchar(50) NOT NULL,
city     varchar(50) NOT NULL,
zip      char(5) NOT NULL,
province varchar(50) NOT NULL,
country  tinyint(3) NOT NULL,

number1  varchar(50) NOT NULL,
number2  varchar(50) NOT NULL,

last_login   TIMESTAMP,    
registered   TIMESTAMP,
online       tinyint(1) unsigned default 0,

admin           tinyint(1) unsigned default 0,
comment_allowed tinyint(1) unsigned default 0,
post_allowed    tinyint(1) unsigned default 0

) ENGINE=InnoDB;

CREATE TABLE future_user
(
username varchar(15) NOT NULL,
password varchar(15) BINARY NOT NULL,
mail     varchar(50) NOT NULL,

name     varchar(50) NOT NULL,
surname  varchar(50) NOT NULL,
birth    char(8) NOT NULL,
sex      tinyint(1) unsigned NOT NULL,

address  varchar(50) NOT NULL,
city     varchar(50) NOT NULL,
zip      char(10) NOT NULL,
province varchar(50) NOT NULL,
country  varchar(50) NOT NULL,

number1  varchar(50) NOT NULL,
number2  varchar(50) NOT NULL,

code     char(10) NOT NULL

) ENGINE=InnoDB;

Use a UNION :使用UNION

SELECT
    fields
FROM
    user
WHERE
    user.username=x
UNION
SELECT
    fields
FROM
    future_user
WHERE
    future_user.username=x

Note that you can't do SELECT * in either because they have different fields.请注意,您不能在任何一个中执行SELECT * ,因为它们具有不同的字段。 You'll need to return the same fields from both subqueries.您需要从两个子查询中返回相同的字段。

It sounds like what you are looking for is a Full Outer Join .听起来您正在寻找的是Full Outer Join Some database systems (notably MySQL) do not support such joins.一些数据库系统(尤其是 MySQL)不支持这种连接。 For those that do, your query would be something like:对于那些这样做的人,您的查询将类似于:

SELECT * FROM user FULL OUTER JOIN future_user
  ON user.username = future_user.username;

For those that don't, you will have to use a UNION of a LEFT JOIN and RIGHT JOIN as explained in the Wikipedia article linked above, something like (in MySQL):对于那些不这样做的人,您将不得不使用 LEFT JOIN 和 RIGHT JOIN 的 UNION,如上面链接的 Wikipedia 文章中所述,类似于(在 MySQL 中):

SELECT * FROM user LEFT JOIN future_user
  ON user.username = future_user.username
UNION
SELECT * FROM user RIGHT JOIN future_user
  ON user.username = future_user.username;

You may want to serious consider if there is an alternative available, such as combining the tables or processing the resultsets from two separate queries on the back end.您可能需要认真考虑是否有可用的替代方法,例如合并表或在后端处理来自两个单独查询的结果集。 FULL OUTER JOINs are very weird to be using, and you're going to end up with a bunch of NULL field values that you'll have to check for and validate. FULL OUTER JOIN 使用起来非常奇怪,您最终会得到一堆 NULL 字段值,您必须检查和验证这些字段值。

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

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