来自多个缺少字段

时间:2017-04-12 09:32:48

标签: mysql sql

希望有人可以帮助我。我有5个表,简化示例如下:

catalog_items :存储有关商品目录中商品的信息。

CREATE TABLE catalog_items (
  item_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  title VARCHAR(255) NOT NULL,
  create_date DATETIME NOT NULL,
  valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,

  PRIMARY KEY (item_id)
);

用户:存储用户信息。

CREATE TABLE users (
  user_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  email VARCHAR(255) NOT NULL,
  valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,

  PRIMARY KEY (user_id)
);

user_contribution_types :用户可以向目录提供商品以获得赠送金额,此表格描述了它的贡献类型以及获得的金额。

CREATE TABLE user_contribution_types (
  type_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  description TEXT NOT NULL,
  credits DECIMAL(5,2) UNSIGNED NOT NULL,
  valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,

  PRIMARY KEY (type_id)
);

user_contributions :与上述所有内容相关的表(用户贡献的x项)。

CREATE TABLE user_contributions (
  user_id INTEGER UNSIGNED NOT NULL,
  item_id INTEGER UNSIGNED NOT NULL,
  type_id INTEGER UNSIGNED NOT NULL,
  create_date DATETIME NOT NULL,
  valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,

  FOREIGN KEY (user_id)
    REFERENCES users(user_id),
  FOREIGN KEY (item_id)
    REFERENCES catalog_items(item_id),
  FOREIGN KEY (type_id)
    REFERENCES user_contribution_types(type_id)
);

pending_items :所有贡献都必须获得批准,因此它们会存储在临时表中。 这些条目可能来自没有帐户的新用户,这就是为什么只有一封电子邮件,而不是user_id

CREATE TABLE pending_items (
  pending_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  email VARCHAR(255) NOT NULL,
  title VARCHAR(255) NOT NULL,
  create_date DATETIME NOT NULL,
  valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,

  PRIMARY KEY (pending_id)
);

问题

我要做的是从目录中选择已批准和待定的项目;然而,到目前为止我提出的最好的方法是将它们分开:

仅选择已批准的贡献:

SELECT a.item_id, c.title, a.create_date, b.description, b.credits
FROM user_contributions a, user_contribution_types b, catalog_items c
WHERE a.type_id = b.type_id AND a.item_id = c.item_id
AND a.valid = TRUE AND b.valid = TRUE AND c.valid = TRUE
ORDER BY c.create_date DESC LIMIT 0, 10

仅选择待定的贡献:

SELECT pending_id, title, create_date FROM pending_items WHERE email = 'example@email.com' AND valid = TRUE
ORDER BY create_date DESC LIMIT 0, 10

有没有办法将上述2个查询组合起来得到如下结果集,其中缺少的字段只显示为NULL

| item_or_pending_id | title | create_date         | description | credits | approved |
| ------------------ | ----- | ------------------- | ----------- | ------- | -------- |
| 14                 | test4 | 2017-04-08 04:41:21 | bar         | 1.00    | true     |
| 14                 | test3 | 2017-04-06 01:23:45 | NULL        | NULL    | false    |
| 11                 | test2 | 2017-02-03 12:30:00 | NULL        | NULL    | false    |
| 9                  | test1 | 2017-01-25 05:16:18 | foo         | 1.00    | true     |

1 个答案:

答案 0 :(得分:2)

是的: - )

SELECT a.item_id AS item_or_pending_id, 
       c.title, 
       a.create_date, 
       b.description, 
       b.credits, 
       true      AS approved 
FROM   user_contributions a, 
       user_contribution_types b, 
       catalog_items c 
WHERE  a.type_id = b.type_id 
       AND a.item_id = c.item_id 
       AND a.valid = true 
       AND b.valid = true 
       AND c.valid = true 
UNION 
SELECT pending_id, 
       title, 
       create_date, 
       NULL, 
       NULL, 
       false 
FROM   pending_items 
WHERE  email = 'example@email.com' 
       AND valid = true 
ORDER  BY create_date DESC 
LIMIT  0, 10