获取所选用户的位置

时间:2015-06-25 13:57:26

标签: php mysql

这是我的查询,

$query=mysql_query("select * 
                    from quiz_grades as grade,
                         user_info_data as info,
                         user as user 
                    where user.id=grade.userid 
                      and user.id=info.userid 
                      and grade.quiz=12 
                      and info.fieldid=1 
                      and info.data='AP'
                    order by data ASC, grade DESC");

从下表中

$USER->id是当前登录的用户变量。

如果userid = $USER->id属于AP,则应显示所有AP。 如果userid = $USER->id属于TL,则应显示所有TL。

info.data存储AP和TL记录。

<table>
  <tr>
    <th>userid</th>
    <th>data</th>
  </tr>
  <tr>
    <td>1</td>
    <td>AP</td>
  </tr>
   <tr>
    <td>2</td>
    <td>TL</td>
  </tr>
   <tr>
    <td>3</td>
    <td>AP</td>
  </tr>
   <tr>
    <td>4</td>
    <td>AP</td>
  </tr>
   <tr>
    <td>5</td>
    <td>TL</td>
  </tr>
   <tr>
    <td>6</td>
    <td>TL</td>
  </tr>
   <tr>
    <td>7</td>
    <td>AP</td>
  </tr>
   <tr>
    <td>8</td>
    <td>TL</td>
  </tr>
   <tr>
    <td>9</td>
    <td>TL</td>
  </tr>
   <tr>
    <td>10</td>
    <td>TL</td>
  </tr>
</table>

先谢谢

2 个答案:

答案 0 :(得分:0)

考虑到user表中有一个位置字段

,您应该在查询中包含该条件,如下所示
select grade.* from quiz_grades as grade
join user as user on user.id=grade.userid and grade.quiz=12
join user_info_data as info on user.id=info.userid  
where `fieldid`=1 
and info.data = 'telengana'
order by data ASC, grade DESC

答案 1 :(得分:0)

假设您有一个要使用的位置字段(locationField):

$query=mysql_query("
  SELECT * 
  FROM   quiz_grades AS grade, 
       user_info_data AS info, 
       USER AS USER 
  WHERE  USER.id = grade.userid 
       AND USER.id = info.userid 
       AND grade.quiz = 12 
       AND ` fieldid `= 1
       AND `location` IN (SELECT location FROM user_info_data  WHERE user_info_data.userid = {$USER->id})
  ORDER  BY data ASC, 
          grade DESC ");