简体   繁体   中英

Remove matching values by comparing two database tables Codeigniter/Mysql

In my codeigniter model I have this function :

public function getUserNo()
{
    $query = $this->db->query("select userno from Users where active=1");
    return $query->result_array();
}

This code outputs the userno from the Users table.

I have another table called Userslist which also contains the userno field.

I want a query such that it first gets all the userno from the Userlist table and then check if they exist in the Users table and if they exist then return all the userno from the Users table except for those which are there in the Userslist table. How do I write this query?

select u.userno FROM Userlist as ui 
LEFT JOIN ON users as u 
u.userno=ui.userno WHERE u.active = 'active' GROUP BY u.userno

您可以使用SQL NOT IN 在此处输入链接描述来使用子查询

select userno from Users where active= 1 AND userno NOT IN (SELECT userno FROM Userlist)

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