2
我有兩個表,一個包含所有用戶數據,另一個包含一些命令。 訂單總是針對一個用戶,但是由另一個用戶訂購(用戶不能爲他自己訂購)MYSQL:連接多個表 - 用用戶名替換多個用戶ID
在訂單表中,編輯器和用戶只存儲其id與用戶表相對應。
這裏的縮寫表:
`orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(10) NOT NULL,
`editor_id` int(10) NOT NULL,
`reason` char(200) COLLATE utf8_unicode_ci NOT NULL,
`amount` decimal(10,2) NOT NULL
)
`users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` char(250) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`password` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`email` varchar(250) COLLATE utf8_unicode_ci NOT NULL DEFAULT ''
)
我想查詢訂單之一,並獲得與USER_NAME取代USER_ID和editor_id也置換成從用戶表中的正確名稱。 這是查詢結果的外觀正常訂單,如:
[0] => Array
(
[id] => 714
[user_id] => 97
[editor_id] => 45
[reason] => Ausgaben Regale 28.09.13
[amount] => 150.00
)
,這就是我想要的:
[0] => Array
(
[id] => 714
[user_id] => 97
[editor_id] => 45
[user_name] => the user name
[editor_name] => the editor name
[reason] => Ausgaben Regale 28.09.13
[amount] => 150.00
)
我嘗試了許多不同的連接,但都沒有成功,很想得到提示指向我正確的方向。
你可以把使用加入查詢之一,你已經嘗試過?您需要兩次將'orders'連接到'users' - 一次用於userid,一次用於編輯器ID – InSane