Mysql内连接,Where,Between在多个表中

时间:2014-05-19 06:38:03

标签: mysql inner-join where between

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_startsession_enduser { {1}},space已设置。但我得到以下错误

  

您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以便在第1行country附近使用正确的语法

'er INNER JOIN countries t4 ON t3.country=t4.id WHERE 1 order by t1.session_star'

1 个答案:

答案 0 :(得分:0)

你在 t3.us和呃之间有空格,我想在所有3个select语句中都应该 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