加入三个mysql表/两个select语句

时间:2012-10-09 10:52:12

标签: mysql

我有以下表格:

http://www.gulllakeschools.net/mysqltables.pdf

enter image description here

我需要以这种格式提取数据: 学生姓氏,学生姓名,date_event,date_event(学生每天都会有一个日期事件)

组件com_users是我需要从组件列中提取的组件。

我是主要的mysql菜鸟,无法弄清楚如何一次性完成所有操作。我有这些单独的select语句:

SELECT civicrm_contact.last_name, civicrm_contact.first_name, civicrm_uf_match.uf_id
FROM civicrm_contact, civicrm_uf_match
WHERE civicrm_contact.id = civicrm_uf_match.contact_id

SELECT user_id, component, group_concat( date_event )
FROM jos_content_statistics
WHERE component = "com_users"
GROUP BY user_id

但我不能为我的生活让他们加入。我尝试过加入,加入,完全加入,联合和所有产生错误。我在where语句之前和之后尝试过连接。 Union产生排序错误。我没有尝试过内部联接,因为我无法想出那个。

3 个答案:

答案 0 :(得分:1)

SELECT C.last_name, C.first_name, S.date_event
FROM civicrm_contact as C
INNER JOIN civicrm_uf_match as M on M.contact_id = C.id 
INNER JOIN jos_content_statistics as S on M.uf_id = S.user_id

答案 1 :(得分:0)

SELECT 
  civicrm_contact.last_name, 
  civicrm_contact.first_name, 
  civicrm_uf_match.uf_id,
  (select group_concat(jos_content_statistics.date_event) 
   from jos_content_statistics 
   where user_id = civicrm_uf_match.uf_id 
     and civicrm_uf_match.contact_id = civicrm_contact.id 
     and component = "com_users" 
   GROUP BY user_id) as date_events
FROM civicrm_contact join civicrm_uf_match
on civicrm_contact.id = civicrm_uf_match.contact_id
where civicrm_contact.contact_sub_type='student' 

答案 2 :(得分:0)

试试这个:

SELECT cc.last_name, cc.first_name, cm.uf_id, jc.component, jc.dateevent 
FROM civicrm_contact cc
INNER JOIN civicrm_uf_match cm ON cc.civicrm_contact.id = cm.contact_id
INNER JOIN
(
    SELECT user_id, component, group_concat( date_event ) dateevent
    FROM jos_content_statistics
    WHERE component = "com_users"
    GROUP BY user_id, component
) jc ON cm.uf_id = jc.user_id