简体   繁体   中英

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

1- checkin_bonus TABLE

| user_id |

| 2   |
| 5   |
| 1   |

2- user_pages TABLE

| 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

My app is MVC Based

 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

 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

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?

if it is user_id, then please replace join on clause with u.user_id and try.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM