Mysql Inner join,Where,Between 在多个表中

Posted

技术标签:

【中文标题】Mysql Inner join,Where,Between 在多个表中【英文标题】:Mysql Inner joint,Where,Between in multiple tables 【发布时间】:2014-05-19 06:38:02 【问题描述】:

user_destinations

id  user    space   active  timestamp   user_id country     
1   2   1   no  1336575701  2   1
2   3   10  no  1337545278  3   1
3   4   1   no  1338233452  1   1
4   5   1   no  1338233460  4   2
5   6   1   no  1341486097  6   1
6   7   1   no  1341486104  7   1

cbeta_sessions

id  ses_id        user_id   ses_start   ses_end     ses_ip       logged_in  
13003   529.42202496529 1   1400135811  1400047840  127.0.0.1   1
13002   530.34401917458 2   1400048662  1400048293      127.0.0.1   1
13001   529.36102104187 3   1400048278  1400048287  127.0.0.1   0
12999   530.35802006721 4   1400047825  1400047842  127.0.0.1   0

国家

id  country    timestamp    user_id     
1   Kenya      1336368145   1
2   Uganda     1336372901   2
3   Tanzania   1336372901   3

cbeta_user

id   names   email             is_admin
1     jick   ji@gmail.com       yes
2     angel  angel@gmail.com    no
3     jim    jim@gmail.com      no

代码:

<?php

function load_session_users()
    

        if($this->status == '1')
        
$rs = sql("SELECT * FROM cbeta_sessions t1 INNER JOIN  cbeta_user  t2 on t1.user_id=t2.id INNER JOIN user_designations t3 ON t1.user_id=t3.user INNER JOIN countries t4 ON t3.country=t4.id WHERE 1 ".(!empty($this->space)?" AND t3.space='$this->space'":"").(!empty($this->country)?" AND t3.country ='$this->country'":""). " AND t1.logged_in=1 AND t1.session_start BETWEEN '$this->from' AND '$this->to' order by t1.session_start desc")or die(mysql_error());
        
        elseif($this->status == '0')
            
$rs = sql("SELECT * FROM cbeta_sessions t1 INNER JOIN cbeta_user t2 on t1.user_id=t2.id INNER JOIN user_designations t3 ON t1.user_id=t3.user INNER JOIN countries t4 ON t3.country=t4.id WHERE 1 ".(!empty($this->space)?" AND t3.space='$this->space'":"")
   .(!empty($this->country)?" AND t3.country ='$this->country'":"")." AND t1.logged_in=0 AND t1.session_start BETWEEN '$this->from' AND '$this->to' order by t1.session_start desc")or die(mysql_error());

        
        elseif($this->status == '')
        

$rs = sql("SELECT * FROM cbeta_sessions t1 INNER JOIN cbeta_user t2 on t1.user_id=t2.id INNER JOIN user_designations t3 ON 1.user_id=t3.user INNER JOIN countries t4 ON t3.country=t4.id WHERE 1 ".(!empty($this->space)?" AND t3.space='$this->space'":"")
.(!empty($this->country)?" AND t3.country ='$this->country'":"").           " order by t1.session_start desc")or die(mysql_error()); 

        
        else
        

$rs = sql("SELECT * FROM cbeta_sessions t1 INNER JOIN cbeta_user t2 on t1.user_id=t2.id INNER JOIN user_designations t3 ON t1.user_id=t3.user_id INNER JOIN countries t4 ON t3.country=t4.id WHERE 1 ".(!empty($this->space)?" AND t3.space='$this->space'":"").(!empty($this->country)?" AND t3.country ='$this->country'":"")." order by 1.session_start desc")or die(mysql_error());
           

        while($row = mysql_fetch_assoc($rs))
        
            $user = new Session_tracker($row['user_id']);
            $this->users [] = $user;

        
        $this->loaded = true;
    
?>

我在 php 中有上述函数用于加载用户会话。我正在检索活动用户、非活动用户或所有正在使用系统的用户,同时检查其他参数是否像 session_start,session_end,user space,country被设置。但我收到以下错误

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'er INNER JOIN countries t4 ON t3.country=t4.id WHERE 1 order by t1.session_star' 附近使用正确的语法

kindly assist me to identify the error

【问题讨论】:

那么,er 在那里做什么? 原来的sql又格式化了一次 【参考方案1】:

t3.us 和 er 之间有空格,我猜在所有 3 个选择语句中应该是 t3.user,所以错误

SELECT * 
FROM cbeta_sessions t1 INNER JOIN  cbeta_user t2 on t1.user_id=t2.id 
INNER JOIN user_designations t3 ON t1.user_id=t3.user 
INNER JOIN countries t4 ON t3.country=t4.id WHERE 1

【讨论】:

再次检查实际的sql,因为我已经格式化好了 是的,错误仍然存​​在。请帮我检查一下

以上是关于Mysql Inner join,Where,Between 在多个表中的主要内容,如果未能解决你的问题,请参考以下文章

Mysql Inner join,Where,Between 在多个表中

在mysql中使用where和inner join

带有 WHERE 子句和 INNER JOIN 的 MySQL 更新查询不起作用

mysql的inner join,left jion,right join,cross join以及on和where的区别

26.MySQL中的内连接INNER JOIN

MySQL Inner Join 有限制吗?