我有4個表是這樣的:MySQL查詢加入多桌
用戶
id | name | type | user | pass | active_work | word_limit | max_word_limit | photo | note
--------------
1 | test1 | 3 | user1 | pass1 | 0 | 10 | 15 | photo1 | note1
2 | test2 | 4 | user2 | pass2 | 3 | 12 | 22 | photo2 | note2
3 | test3 | 4 | user3 | pass3 | 1 | 12 | 22 | photo3 | note3
4 | test4 | 3 | user4 | pass4 | 2 | 12 | 22 | photo4 | note4
5 | test5 | 3 | user5 | pass5 | 2 | 12 | 22 | photo5 | note5
6 | test6 | 3 | user6 | pass6 | 3 | 12 | 22 | photo6 | note6
假期(我的問題就在這裏,有桌子!)
id | user_id | from_date | till_date
--------------
3 | 1 | 16-03-2017 | 22-03-2017
4 | 1 | 25-03-2017 | 27-03-2017
tr_cat
id | user_id | cat_id
--------------
1 | 1 | 1
2 | 2 | 1
1 | 1 | 2
2 | 2 | 2
tr_type
id | user_id | type_id
--------------
1 | 1 | 1
2 | 2 | 1
2 | 2 | 2
,也許你需要知道什麼是type
和cat
貓
id | name
--------------
1 | English
2 | Arabic
3 | Germany
型
id | name
--------------
1 | normal
2 | special
USER_TYPE
id | name
--------------
1 | admin
2 | member
3 | freelancer
4 | full time
描述: 所有表被連接。
我嘗試在tr_cat
和tr_type
特殊cat_id
和type_id
選擇用戶,其可用於前(指可以將特殊的語言和類型)
:select * from users,tr_cat,tr_type where tr_type.user_id = users.id AND tr_cat.user_id = user.id AND tr_cat.id = 1 AND tr_type.id = 1
而且我想user.id
NOT EXISTS在vacation
列表或者是否存在該用戶的行.id TODAY DATE
NOT BETWEEN from_date
和till_date
(表示用戶在工作時可用!)
ð最後我想user.type
和user.active_work
訂購用於創建在我的項目的查詢,我從PHP-MySQLi-Database-Class使用,無論如何,如果你能幫助我理解普通查詢過: - 」
這裏是我的代碼:
$db->where ("(u.type = ? or u.type = ?)", Array(3,4));
$db->join("vacation v", $today." NOT BETWEEN v.from_date and v.till_date", "LEFT");
// problem here i don't know how to join this tables
$db->joinWhere("vacation v", "v.user_id=u.id");// OR User ID Not EXISTS
$db->join("tr_cat c", "c.user_id=u.id", "LEFT");
$db->joinWhere("tr_cat c", "c.id", $cat);
$db->join("tr_type t", "t.user_id=u.id", "LEFT");
$db->joinWhere("tr_type t", "t.id", $type);
$tr_list = $db->get ("users u", null, "u.id, u.name, u.type, u.user, u.active_work, u.word_limit, u.max_word_limit");
任何人都可以幫助我進行此連接?
我想造成這樣的事情
id | name | type | user | active_work | word_limit | max_word_limit
--------------
3 | test3 | 4 | user3 | 1 | 12 | 22
2 | test2 | 4 | user2 | 3 | 12 | 22
5 | test5 | 3 | user5 | 2 | 12 | 22