2011-11-08 65 views
1

我有4個表:查詢隨機返回的所有項目或只是一些項目

mysql> describe solution_sections; 
+---------------------+---------------+------+-----+---------+----------------+ 
| Field    | Type   | Null | Key | Default | Extra   | 
+---------------------+---------------+------+-----+---------+----------------+ 
| solution_section_id | int(10)  | NO | PRI | NULL | auto_increment | 
| display_order  | int(10)  | NO |  | NULL |    | 
| section_name  | varchar(1000) | YES |  | NULL |    | 
+---------------------+---------------+------+-----+---------+----------------+ 
3 rows in set (0.00 sec) 

mysql> describe suggested_solution_comments; 

+-----------------------+----------------+------+-----+---------+----------------+ 
| Field     | Type   | Null | Key | Default | Extra   | 
+-----------------------+----------------+------+-----+---------+----------------+ 
| comment_id   | int(10)  | NO | PRI | NULL | auto_increment | 
| problem_id   | int(10)  | NO |  | NULL |    | 
| suggested_solution_id | int(10)  | NO |  | NULL |    | 
| commenter_id   | int(10)  | NO |  | NULL |    | 
| comment    | varchar(10000) | YES |  | NULL |    | 
| solution_part   | int(3)   | NO |  | NULL |    | 
| date     | date   | NO |  | NULL |    | 
+-----------------------+----------------+------+-----+---------+----------------+ 


mysql> describe users; 
+--------------+---------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+--------------+---------------+------+-----+---------+----------------+ 
| user_id  | int(10)  | NO | PRI | NULL | auto_increment | 
| first_name | varchar(100) | NO |  | NULL |    | 
| last_name | varchar(100) | NO |  | NULL |    | 
| email  | varchar(150) | NO |  | NULL |    | 
| user_pass | varchar(40) | NO |  | NULL |    | 
| zip   | varchar(100) | NO |  | NULL |    | 
| country  | varchar(100) | NO |  | NULL |    | 
| city   | varchar(100) | NO |  | NULL |    | 
| state  | varchar(100) | NO |  | NULL |    | 
| lat   | float(9,6) | YES |  | NULL |    | 
| lng   | float(9,6) | YES |  | NULL |    | 
| agreed_terms | tinyint(1) | YES |  | NULL |    | 
| join_date | date   | NO |  | NULL |    | 
| last_login | date   | NO |  | NULL |    | 
| bio_blurb | varchar(5000) | YES |  | NULL |    | 
+--------------+---------------+------+-----+---------+----------------+ 
15 rows in set (0.03 sec) 

mysql> describe member_photo; 
+-------------------+---------------+------+-----+---------+----------------+ 
| Field    | Type   | Null | Key | Default | Extra   | 
+-------------------+---------------+------+-----+---------+----------------+ 
| photo_id   | int(10)  | NO | PRI | NULL | auto_increment | 
| member_id   | int(10)  | NO |  | NULL |    | 
| photo_description | varchar(3000) | YES |  | NULL |    | 
| photo_path  | varchar(1000) | NO |  | NULL |    | 
| small_thumb  | varchar(1000) | YES |  | NULL |    | 
| mid_thumb   | varchar(1000) | YES |  | NULL |    | 
| is_main_photo  | tinyint(1) | YES |  | NULL |    | 
+-------------------+---------------+------+-----+---------+----------------+ 

我有這樣的查詢:

select comment_id, 
     commenter_id, 
     section_name, 
     comment, 
     solution_part, 
     display_order, 
     solution_section_id, 
     suggested_solution_id, 
     DAYOFMONTH(date), 
     DAYNAME(date), 
     YEAR(date), 
     MONTH(date), 
     first_name, 
     last_name, 
     email, 
     small_thumb, 
     mid_thumb 
from solution_sections 
left join suggested_solution_comments on 
    solution_sections.solution_section_id = suggested_solution_comments.solution_part 
left join users on 
    suggested_solution_comments.commenter_id = users.user_id 
left join member_photo on 
    suggested_solution_comments.commenter_id = member_photo.member_id 
where suggested_solution_id = 61 OR 
     suggested_solution_id IS NULL 
order by solution_section_id, 
     comment_id, 
     section_name, 
     comment, 
     solution_part, 
     display_order; 

什麼它應該做的就是從每個SECTION_NAME solution_sections表,然後找到評論(以及關於評論者的數據)。有時候沒有評論,但它仍然應該至少返回帶有section_name的行,而其他所有內容都是null。

但由於某種原因,它沒有。而最奇怪的部分是,如果我給它一個不同的suggested_solution_id來匹配,它將返回所有的solution_sections行。

任何想法爲什麼會發生這樣的事情?謝謝!!

而我剛剛意識到一件事 - 如果針對任何problem_id做了另一個評論,則此查詢不會返回該部分的行。

+0

我現在正在測試它,看起來該子句是錯誤的地方,但我不確定它究竟有什麼問題。 – GeekedOut

回答

1

當子記錄不保證退出時,您需要左外部聯接來查看父表中的所有記錄。我還避免增加where子句時使用外連接的。我認爲它更具有可讀性,讓您的加入子查詢中,並篩選結果。。嘗試是這樣的:

select * from 
(
    select sc.comment_id, 
      sc.commenter_id, 
      ss.section_name, 
      sc.comment, 
      sc.solution_part, 
      ss.display_order, 
      ss.solution_section_id, 
      sc.suggested_solution_id, 
      DAYOFMONTH(sc.date), 
      DAYNAME(sc.date), 
      YEAR(sc.date), 
      MONTH(sc.date), 
      u.first_name, 
      u.last_name, 
      u.email, 
      mp.small_thumb, 
      mp.mid_thumb 
    from solution_sections ss 
    left outer join suggested_solution_comments sc on ss.solution_section_id = sc.solution_part 
    left outer join users u on sc.commenter_id = u.user_id 
    left outer join member_photo mp on sc.commenter_id = mp.member_id) a 
where a.suggested_solution_id = 61 OR 
     a.suggested_solution_id IS NULL 
order by a.solution_section_id, 
     a.comment_id, 
     a.section_name, 
     a.comment, 
     a.solution_part, 
     a.display_order; 

編輯:

select sc.comment_id, 
     sc.commenter_id, 
     ss.section_name, 
     sc.comment, 
     sc.solution_part, 
     ss.display_order, 
     ss.solution_section_id, 
     sc.suggested_solution_id, 
     DAYOFMONTH(sc.date), 
     DAYNAME(sc.date), 
     YEAR(sc.date), 
     MONTH(sc.date), 
     u.first_name, 
     u.last_name, 
     u.email, 
     mp.small_thumb, 
     mp.mid_thumb 
from solution_sections ss 
left outer join suggested_solution_comments sc on ss.solution_section_id = sc.solution_part 
               AND sc.suggested_solution_id = 61 
left outer join users u on sc.commenter_id = u.user_id 
left outer join member_photo mp on sc.commenter_id = mp.member_id 
order by solution_section_id, 
     comment_id, 
     section_name, 
     comment, 
     solution_part, 
     display_order; 
+0

,它給出了一個錯誤消息「每個交付的表都必須有它自己的別名」 – GeekedOut

+0

這看起來和我原來的完全一樣,除了表別名,不是嗎? – GeekedOut

+0

哎呀,現在再試一次。 – StevieG

1

如果你想顯示solution_sections即使所有的休息不存在,你可以使用「左加盟」:

select comment_id,commenter_id, section_name, comment, solution_part, 
    display_order, solution_section_id, suggested_solution_id, 
    DAYOFMONTH(date), DAYNAME(date), YEAR(date), MONTH(date), 
    first_name, last_name, email, small_thumb,mid_thumb 
from solution_sections 
left outer join suggested_solution_comments on solution_sections.solution_section_id = suggested_solution_comments.solution_part 
    and suggested_solution_id = 61 
left outer join users on suggested_solution_comments.commenter_id = users.user_id 
left outer join member_photo on suggested_solution_comments.commenter_id = member_photo.member_id 
where solution_section_id = ???? 
order by solution_section_id, comment_id, section_name, comment, solution_part,display_order; 

PS。嘗試使用別名表更容易讀取:-)

+0

謝謝 - 你能提醒我外連接的含義是什麼嗎?長時間沒有使用這些lol – GeekedOut

+0

只是嘗試了外連接解決方​​案,但它仍然不起作用 – GeekedOut

+1

['left join'和'left outer join'是同一件事](http://stackoverflow.com/questions/406294/left-join-and-left-outer-join-in-sql-server) –

相關問題