2012-11-16 52 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

也可能是由于使用不正确的数据库或模式名称导致的。