2013-04-08 55 views
3

表用戶MYSQL - 從表1和表中所有行的所有可能性2

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

我覺得最明顯的方法是使用子查詢和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_idpage_id中的select子句來自驅動程序表,而不是來自user_pages,因爲後者可能是NULL

+0

我認爲這是正確的,+1 – fthiella 2013-04-08 18:21:23

+0

這確實是我所看到的,只是需要在ON子句中使用驅動程序這一切工作>選擇driver.user_id,driver.page_id,up.html > from(選擇u.id as user_id,p.id as page_id > from users u cross join > pages p >)driver left outer加入 > user_pages up > on.user_id = driver.user_id and up.page_id = driver.page_id; – neilakapete 2013-04-08 18:35:24

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 
+0

我假設你想顯示來自user_pages表的HTML而不是頁面,是否正確? – Marjeta 2013-04-08 17:55:43

+0

使用此查詢我收到'unknown column u.id – neilakapete 2013-04-08 18:23:23

+0

您是否剪切並粘貼它,或者您是否鍵入了它?因爲,你有桌子「用戶」,對吧?而且我用名稱「u」(FROM users u)對此表進行別名,然後引用此表中的「id」列(ON u.id = up.user_id)。而且,「id」是表格「用戶」中的有效列,對吧?只是想通過驗證我的假設來弄清楚爲什麼這段代碼不起作用... – Marjeta 2013-04-19 16:23:02

1

代碼波紋管是MySQL服務器上測試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; 
相關問題