[英]get results from mysql table while matching results from another
I am trying to get result if id match in both tables, currently i have 2 table with structure below 我试图在两个表中的id匹配的情况下得到结果,目前我有2个结构如下的表
1- checkin_bonus TABLE 1- checkin_bonus表
| user_id |
| 2 |
| 5 |
| 1 |
2- user_pages TABLE 2个user_pages表
| user_id | score |
| 2 | 100 |
| 3 | 300 |
| 6 | 600 |
Desire Results: if user_id of table checkin_bonus match any user_pages user_id return result in example case user_id & score = 100 should displyed i am doing something like this but no success 期望的结果:如果表checkin_bonus的user_id与任何user_pages user_id返回结果相匹配,例如在user_id&score = 100的示例中,我应该这样做,但没有成功
My app is MVC Based 我的应用程序基于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>
It will help me alot. 它将对我有很大帮助。
INNER JOIN is what you are looking for 寻找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
You should use Join
for this 您应该为此使用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
try this just use 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
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
Actually whats your field name?? 其实您的字段名称是什么? Is it user_id or users_id? 是user_id还是users_id?
if it is user_id, then please replace join on clause with u.user_id and try. 如果是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
You should use below query to get your desired result. 您应该使用以下查询来获得所需的结果。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.