从匹配来自另一个的结果获取mysql表的结果

时间:2016-05-31 11:57:36

标签: php mysql

如果两个表中的id匹配,我试图获得结果,目前我有2个表格,结构在

之下

1- checkin_bonus

| user_id |

| 2   |
| 5   |
| 1   |

2- user_pages

| user_id | score |

| 2       |    100  |
| 3       |    300  |
| 6       |    600  |

欲望结果:如果表checkin_bonus的user_id与示例案例中的任何user_pages user_id返回结果匹配 user_id&得分= 100 应该显示出来 我正在做这样的事但没有成功

我的应用是基于MVC的

 Model.php

// Checkin Pages
       public function checkinpagesL()
{
return $this->db->select('SELECT user_id FROM checkin_bonus AS c 
AND user_id FROM user_pages AS u JOIN WHERE c.user_id = u.users_id ');

}

View.php
  <?php
                foreach($this->checkinpagesL as $key => $value) {?>
              <tbody>
                <tr> 
                <td><?php echo $value['user_id']?></td>
                <td><?php echo $value['score']?></td>
            </tr>
              </tbody>
              <?php }?>
  </table>

它会帮助我很多。

6 个答案:

答案 0 :(得分:0)

INNER JOIN就是你要找的东西

SELECT * FROM checkin_bonus cb
INNER JOIN user_pages up ON cb.user_id = u.users_id 
where cb.user_id > 18 ORDER BY cb.user_id DES

答案 1 :(得分:0)

您应该使用Join来实现此目标

SELECT c.user_id,u.users_id FROM checkin_bonus AS c 
INNER JOIN user_pages AS u ON c.user_id = u.users_id 
where c.user_id > 18 ORDER BY c.user_id DESC

答案 2 :(得分:0)

试试这个只需使用join

SELECT cb.user_id,up.user_id FROM checkin_bonus as cb
 JOIN user_pages as up ON cb.user_id = up.user_id 
 where cb.user_id > 18 ORDER BY cb.user_id DESC

答案 3 :(得分:0)

SELECT u.user_id as id FROM checkin_bonus as c join user_pages as u on c.user_id =u.user_id WHERE c.user_id>18 ORDER BY c.user_id DESC

答案 4 :(得分:0)

其实你的字段名称是什么?是user_id还是users_id?

如果是user_id,那么请用u.user_id替换join on子句并尝试。

SELECT u.user_id as id FROM checkin_bonus AS c 
JOIN user_pages AS u ON c.user_id = u.user_id 
where c.user_id > 18 ORDER BY c.user_id DESC

答案 5 :(得分:0)

您应该使用以下查询来获得所需的结果。

SELECT A.user_id, SUM(B.score) as total
FROM checkin_bonus A 
JOIN user_pages B 
ON A.user_id = B.user_id
GROUP BY A.user_id