如何在多列中对我的用户表进行SQL连接?

时间:2015-06-03 18:52:28

标签: mysql sql

我有一个填充DataTable的查询。数据库表有多列,列出了我的用户表中的ID。它们是case_officer,created_by,modified_by和location_user_id。每当我处理其中一个列时,我想从user_account表而不是user_id返回用户的Rank和Last Name。我迷失在Concat和Join语言中。以下是我到目前为止的情况:

SELECT SQL_CALC_FOUND_ROWS item.item_id
     , item.item_code
     , item.check_out_flag
     , item.created_by
     , item.creation_date
     , CONCAT(user_account.rank, " ", user_account.last_name) AS case_officer
     , lookup_casetype.short_description AS case_type
     , item.incident_number
     , lookup_itemstatus.short_description AS item_status
     , lookup_finaldisposition.short_description AS final_disposition
     , lookup_danger.short_description AS danger_flag
     , lookup_mso.short_description AS most_serious_offense
     , item.item_description
     , lookup_itemtype.short_description AS item_type
     , lookup_drugpackaging.short_description AS drugs_packaging_type
     , item.drugs_substance, item.drugs_weight
     , item.make, item.model, item.caliber, item.serial, item.money_value
     , item.year, item.registration
     , lookup_state.short_description AS registration_state
     , lookup_color.short_description AS color, item.name_1
     , item.name_2, item.name_3, item.name_4
     , lookup_nametype.short_description AS name_1_type
     , lookup_nametype.short_description AS name_2_type
     , lookup_nametype.short_description AS name_3_type
     , lookup_nametype.short_description AS name_4_type
     , item.note, item.recorder_data, item.recovery_info
     , location.short_description AS location_id
     , lookup_itemclass.short_description AS item_class_id
     , CONCAT(user_account.rank, " ", user_account.last_name) AS modified_by
     , item.modified_date, location_user_id, item.deleted_flag, item.recovery_datetime
FROM item
LEFT JOIN lookup_itemstatus ON lookup_itemstatus.item_status_id = item.item_status
LEFT JOIN lookup_casetype ON lookup_casetype.item_type_id = item.case_type
LEFT JOIN lookup_finaldisposition ON lookup_finaldisposition.finaldisposition_id = item.final_disposition
LEFT JOIN lookup_danger ON lookup_danger.danger_id = item.danger_flag
LEFT JOIN lookup_mso ON lookup_mso.mso_id = item.most_serious_offense
LEFT JOIN lookup_itemtype ON lookup_itemtype.item_type_id = item.item_type
LEFT JOIN lookup_drugpackaging ON lookup_drugpackaging.drug_packaging_id = item.drugs_packaging_type
LEFT JOIN lookup_state ON lookup_state.state_id = item.registration_state
LEFT JOIN lookup_color ON lookup_color.color_id = item.color
LEFT JOIN lookup_nametype ON lookup_nametype.name_type_id = item.name_1_type AND item.name_2_type AND item.name_3_type AND item.name_4_type
LEFT JOIN location ON location.location_id = item.location_id
LEFT JOIN lookup_itemclass ON lookup_itemclass.item_class_id = item.item_class_id
LEFT JOIN user_account ON (user_account.user_account_id = item.case_officer)

ORDER BY item.item_code DESC
LIMIT 0, 150

...适用于case_officer,但我不确定如何为modified_by,created_by和location_user_id做同样的事情而没有错误。

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

基本上是How do I join same table multiple times?

的副本

在连接列表中重复表名,并在表名之后和ON连接谓词之前使用ALIAS。您还可以在字段列表表达式中使用ALIAS:

SELECT SQL_CALC_FOUND_ROWS item.item_id, item.item_code, item.check_out_flag, item.created_by, item.creation_date
, CONCAT(user_account.rank, " ", user_account.last_name) AS case_officer
-- here are the other user references:
, CONCAT(modified_by_user.rank, " ", modified_by_user.last_name) AS ModifiedByWho
, CONCAT(created_by_user.rank, " ", created_by_user.last_name) AS created_by_who
, CONCAT(location_user.rank, " ", location_user.last_name) AS location_Who
, lookup_casetype.short_description AS case_type, item.incident_number, lookup_itemstatus.short_description AS item_status, lookup_finaldisposition.short_description AS final_disposition, lookup_danger.short_description AS danger_flag, lookup_mso.short_description AS most_serious_offense, item.item_description, lookup_itemtype.short_description AS item_type, lookup_drugpackaging.short_description AS drugs_packaging_type, item.drugs_substance, item.drugs_weight, item.make, item.model, item.caliber, item.serial, item.money_value, item.year, item.registration, lookup_state.short_description AS registration_state, lookup_color.short_description AS color, item.name_1, item.name_2, item.name_3, item.name_4, lookup_nametype.short_description AS name_1_type, lookup_nametype.short_description AS name_2_type, lookup_nametype.short_description AS name_3_type, lookup_nametype.short_description AS name_4_type, item.note, item.recorder_data, item.recovery_info, location.short_description AS location_id, lookup_itemclass.short_description AS item_class_id, CONCAT(user_account.rank, " ", user_account.last_name) AS modified_by, item.modified_date, location_user_id, item.deleted_flag, item.recovery_datetime
                 FROM item
                        LEFT JOIN lookup_itemstatus ON lookup_itemstatus.item_status_id = item.item_status
                        LEFT JOIN lookup_casetype ON lookup_casetype.item_type_id = item.case_type
                        LEFT JOIN lookup_finaldisposition ON lookup_finaldisposition.finaldisposition_id = item.final_disposition
                        LEFT JOIN lookup_danger ON lookup_danger.danger_id = item.danger_flag
                        LEFT JOIN lookup_mso ON lookup_mso.mso_id = item.most_serious_offense
                        LEFT JOIN lookup_itemtype ON lookup_itemtype.item_type_id = item.item_type
                        LEFT JOIN lookup_drugpackaging ON lookup_drugpackaging.drug_packaging_id = item.drugs_packaging_type
                        LEFT JOIN lookup_state ON lookup_state.state_id = item.registration_state
                        LEFT JOIN lookup_color ON lookup_color.color_id = item.color
                        LEFT JOIN lookup_nametype ON lookup_nametype.name_type_id = item.name_1_type AND item.name_2_type AND item.name_3_type AND item.name_4_type
                        LEFT JOIN location ON location.location_id = item.location_id
                        LEFT JOIN lookup_itemclass ON lookup_itemclass.item_class_id = item.item_class_id
                        LEFT JOIN user_account ON (user_account.user_account_id = item.case_officer)
                        -- join to other users with ALIAS
                         LEFT JOIN user_account AS modified_by_user ON (modified_by_user.user_account_id = item.modified_by)
                         LEFT JOIN user_account created_by_user ON (created_by_user.user_account_id = item.created_by)
                         LEFT JOIN user_account location_user ON (location_user.user_account_id = item.location_user_id)

                 ORDER BY item.item_code DESC
                 LIMIT 0, 150   

请注意,表别名的AS关键字是可选的。 我在这里使用的别名是令人讨厌的长。许多人会使用短的别名,如umod,ucreated,uloc等。

答案 1 :(得分:0)

看起来您需要第二次加入user_account表。至少有一个引用需要表别名来消除引用的歧义。我为co引用的case_officermb引用的modified_by提供了别名。对于同一个表的其他连接遵循相同的模式。

e.g。

SELECT ...
     , ...
     , CONCAT(co.rank, " ", co.last_name) AS case_officer
     , ...
     , CONCAT(mb.rank, " ", mb.last_name) AS modified_by
     , ...
     , CONCAT(cb.rank, " ", cb.last_name) AS created_by
     , ...
     , CONCAT(lu.rank, " ", lu.last_name) AS location_user_id
     , ...

FROM ...
LEFT JOIN user_account co ON (co.user_account_id = item.case_officer)
LEFT JOIN user_account mb ON (mb.user_account_id = item.modified_by)
LEFT JOIN user_account cb ON (cb.user_account_id = item.created_by)
LEFT JOIN user_account lu ON (lu.user_account_id = item.location_user_id)