MYSQL - 表1和表2中所有可能的所有行

时间:2013-04-08 17:45:49

标签: mysql join

表用户

id, name
1, Jay Bob
2, An Other

表格页

id, name, html
1, 'Welcome', 'Welcome to this page'
2, 'Goodbye', 'Thanks for visiting'

表user_pages **存储用户特定版本的页面**

user_id, page_id, html
1,        1,      'User id 1 Welcome page'

我基本上需要一个查询,它会返回下面的数据集 - 即使没有数据也可以为每种可能性提供一行。

数据集

user_id, page_id, html
1,        1,      'User is 1 Welcome page'
1,        2,      ''
2,        1,      ''
2,        2,      ''

3 个答案:

答案 0 :(得分:3)

我认为最明确的方法是使用子查询和cross join来创建驱动程序表:

select driver.user_id, driver.page_id, up.html
from (select u.id as user_id, p.id as page_id
      from users u cross join
           pages p
     ) driver left outer join
     user_pages up
     on up.user_id = driver.user_id and up.page_id = driver.page_id;

user_id子句中的page_idselect来自驱动程序表而不是来自user_pages,这一点非常重要,因为后者可能是NULL }。

答案 1 :(得分:1)

代码bellow在MySQL Server 5.6上测试过 我希望/假设您的意图接近以下内容:

USE test;

CREATE TABLE users (
id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(80) NOT NULL);

CREATE TABLE pages (
id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR(40) NOT NULL,
html VARCHAR(100) NOT NULL);

CREATE TABLE user_pages (
user_id INT UNSIGNED NOT NULL,
page_id INT UNSIGNED NOT NULL,
html VARCHAR(50) DEFAULT '',
PRIMARY KEY (user_id, page_id));

INSERT users (`name`) VALUES
  ('User 1'), ('User 2'), ('User 3');

INSERT pages (`name`, `html`) VALUES
  ('Welcome','Welcome to this page'),
  ('Goodbye','Thanks for visiting');

INSERT user_pages (user_id, page_id, html) VALUES
  (1,1,"First user's welcome page"),
  (1,2,"First user's goodbye page"),
  (2,1,"Second user's welcome page");

SELECT DISTINCT
  u.id  AS `u_id`,
  u.`name` AS `u_name`,
  IFNULL(x.page_id, p.id)  AS `p_id`,
  IFNULL(p.`name`,'') AS `p_name`,
  IFNULL(x.html,p.html) AS `p_html`
FROM users AS u
CROSS JOIN pages AS p
LEFT OUTER JOIN user_pages AS x
  ON (x.user_id = u.id AND x.page_id = p.id);

上述代码应返回每个用户的特定页面(如果存在)或常规页面。您还可以创建一个视图,并从中查询,就好像它是一个普通的表:

CREATE OR REPLACE VIEW `user_page_details` AS
SELECT DISTINCT
  u.id  AS `u_id`,
  u.`name` AS `u_name`,
  IFNULL(x.page_id, p.id)  AS `p_id`,
  IFNULL(p.`name`,'') AS `p_name`,
  IFNULL(x.html,p.html) AS `p_html`
FROM users AS u
CROSS JOIN pages AS p
LEFT OUTER JOIN user_pages AS x
  ON (x.user_id = u.id AND x.page_id = p.id);

完成后,您的查询将非常简单:

SELECT * FROM user_page_details;

甚至

SELECT * FROM user_page_details WHERE u_id = 2;

答案 2 :(得分:0)

试试这个:

SELECT up.user_id, up.page_id, up.html
FROM users u,pages p
LEFT OUTER JOIN user_pages up ON u.id=up.user_id AND p.id=up.user_id