2012-11-16 51 views
1

我有3個表。我想按日期順序選擇他們的數據。我將所有表格中的日期保存在unixtimestamp中。我使用下面的查詢:sql查詢從多個表中選擇數據

select 
    c.up_date, c.user_id, c.id, 
    f.id, f.up_date, f.friend1, f.friend2, f.status, 
    s.postdate, s.status, s.id, s.display_name, s.userid, s.userid1 
from 
    c.cover_pic, 
    s.wp_userstatus, 
    f.wp_friends 
where 
    s.userid=c.friend1 
    and s.userid=c.user_id 
    and f.status=1 
    and c.user_id=4 
order by s.postdate 

Tablel結構爲:cover_pic表:

id user_id coverpic        color up_date 
1 4   496b02165600889daf51c6b04b257ec0.jpg 63ACFF 1353069741 

wp_friends表:

id friend1 friend2 status up_date 
12 1 4 2  1353064093 
11 4 1 1  1353064093 

wp_userstatus表:

id status display_name userid userid1 postdate  
6 awesome paramveer  4  4  1352414658 
7 lets  paramveer  4  4  1352414932 

它顯示了以下錯誤:

#1142 - SELECT command denied to user 'kdgadget'@'localhost' for table 'cover_pic' 

我想要按日期顯示順序的數據。

+1

和您當前的查詢有什麼問題? –

+0

你想要什麼輸出? – Arion

+0

使用連接使您的查詢可讀。 – Farfarak

回答

0
(SELECT * from in_order where `id` = '$id') 
UNION 
(SELECT * from pre_order where `id` = '$id') 

但是每個表必須是相同的長度,大小。

0

使用此查詢,它使用聯接和表的別名被組織:

select c.up_date,c.user_id,c.id,f.id,f.up_date,f.friend1,f.friend2,f.status, 
s.postdate, s.status,s.id,s.display_name.s.userid,s.userid1 
from cover_pic as c JOIN wp_userstatus as s, 
ON s.userid=c.friend1 and s.userid=c.user_id and c.user_id=4 
JOIN wp_friends as f ON f.status=1 
order by s.postdate 

你的錯誤不是從SQL。用戶沒有執行select的權限。 看到錯誤解決的帖子:

  1. User cannot execute select error 1142
  2. ERROR 1142 (42000): ALTER command denied
  3. MySQL Error: #1142 - SELECT command denied to user
+0

它不起作用 – user1734190

2

SELECT command denied to user 'kdgadget'@'localhost' for table 'cover_pic'

應該是一個明確的信息。用戶kdgadget可能不會在表cover_pic上執行SELECT命令。所以這是一個數據庫配置問題,而不是查詢問題。

0

也可能是由於使用不正確的數據庫或模式名稱導致的。