简体   繁体   English

sql查询从多个表中选择数据

[英]sql query to select data from multiple table

I have 3 table.我有3张桌子。 I want to select the data from them order by date.我想按日期从它们中选择数据。 I'm saving the date in unixtimestamp in all table.我正在所有表中以 unixtimestamp 格式保存日期。 I'm using the following query:我正在使用以下查询:

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 structure is: cover_pic table:表结构为: cover_pic表:

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

wp_friends table: wp_friends表:

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

wp_userstatus table: wp_userstatus表:

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

It is showing the following error:它显示以下错误:

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

I want to show the data in order by date.我想按日期顺序显示数据。

SELECT command denied to user 'kdgadget'@'localhost' for table 'cover_pic' SELECT 命令拒绝用户 'kdgadget'@'localhost' 用于表 'cover_pic'

should be a clear message.应该是一个明确的信息。 User kdgadget may not execute a SELECT command on table cover_pic.用户 kdgadget 可能不会对表 cover_pic 执行 SELECT 命令。 So it's a database configuration issue, not a query one.所以这是一个数据库配置问题,而不是一个查询问题。

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

but each table must be same length, size.但每张桌子的长度、大小必须相同。

Use this query which uses joins and alias of tables are organized:使用这个查询,它使用连接和表的别名被组织:

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

Your error is not from sql.您的错误不是来自 sql。 User don't have permissions to execute select.用户没有执行选择的权限。 See error solving posts:请参阅错误解决帖子:

  1. User cannot execute select error 1142 用户无法执行选择错误 1142
  2. ERROR 1142 (42000): ALTER command denied 错误 1142 (42000):ALTER 命令被拒绝
  3. MySQL Error: #1142 - SELECT command denied to user MySQL 错误:#1142 - 拒绝用户的 SELECT 命令

可能的原因之一可能是使用了不正确的数据库或架构名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM