简体   繁体   English

MySql 通过 user_id 获取页面列表,或者如果未定义 user_id 获取所有页面

[英]MySql get list of pages by user_id or if user_id is not defined get all pages

I would like to get a list of pages from my MySQL table.我想从我的 MySQL 表中获取页面列表。 But I want to get a list of pages added by all users if the $user_id value is NULL and if the $user_id value is defined I need to get a list of pages added by that particular user.但是,如果 $user_id 值为 NULL 并且如果定义了 $user_id 值,我想获取所有用户添加的页面列表,我需要获取该特定用户添加的页面列表。 I feel like there may be a simple solution for this, but I have tried to find a solution for this kind of situation for a long time and I failed with approaches such as "WHERE user_id is NOT NULL OR user_id = $user_id".我觉得可能有一个简单的解决方案,但我长期以来一直试图为这种情况找到解决方案,但我用诸如“WHERE user_id is NOT NULL OR user_id = $user_id”之类的方法失败了。 How do I need to modify the below code to obtain expected results?如何修改以下代码以获得预期结果?

public function getPagesList($user_id = NULL){

  $this->db->query ('
    SELECT `page_id`, `page_name`
    FROM `pages`
    WHERE `user_id` = :user_id

');

$this->db->bind(':user_id', $user_id);

return $this->db->resultSet();
}

you can use a wildcard with LIKE您可以在 LIKE 中使用通配符

as Liam Sorsby pointed correctly all Pages could be a lot of pages, so you should be thinking about pagonation or Limiting the number of rows正如利亚姆·索斯比(Liam Sorsby)正确指出的那样,所有页面都可能是很多页面,因此您应该考虑分页或限制行数

public function getPagesList($user_id = NULL){
   $uid = is_null($user_id) ?  '%' : $user_id;
  $this->db->query ('
    SELECT `page_id`, `page_name`
    FROM `pages`
    WHERE `user_id` LIKE :user_id

');

$this->db->bind(':user_id', $uid);

return $this->db->resultSet();
}

This works like so这就像这样

create table numericstring ( pk INT auto_INCREMENT PRIMARY KEY, id INT );
 insert into numericstring (id) values ( 20001 ),( 20002 ),( 30001 ),( 30002 ),( 30003 ),( 30004 ),( 37 ),( 937 ),( 21 ),( 3 ),( 222222 ),( 3333 );
 select * from numericstring WHERE id LIKE '3'
 pk | PK | id -: | id -: | -: 10 | -: 10 | 3 3
 select * from numericstring WHERE id LIKE 3
 pk | PK | id -: | id -: | -: 10 | -: 10 | 3 3
 select * from numericstring WHERE id LIKE '%'
 pk | PK | id -: | id -: | -----: 1 | -----: 1 | 20001 2 | 20001 2 | 20002 3 | 20002 3 | 30001 4 | 30001 4 | 30002 5 | 30002 5 | 30003 6 | 30003 6 | 30004 7 | 30004 7 | 37 8 | 37 8 | 937 9 | 937 9 | 21 10 | 21 10 | 3 11 | 3 11 | 222222 12 | 222222 12 | 3333 3333

db<>fiddle here db<> 在这里摆弄

I want to get a list of pages added by all users if the $user_id value is NULL and if the $user_id value is defined I need to get a list of pages added by that particular user.如果 $user_id 值为 NULL 并且如果定义了 $user_id 值,我想获取所有用户添加的页面列表,我需要获取该特定用户添加的页面列表。

This would translate as:这将翻译为:

WHERE :user_id is NULL OR user_id = :user_id

When the parameter is null , this returns all rows;当参数为null时,返回所有行; when the parameter is set, it is used for filtering column user_id .设置参数时,用于过滤列user_id

You can shorten the expression a little with coalesce() , but it might be less efficient:您可以使用coalesce()稍微缩短表达式,但它可能效率较低:

WHERE COALESCE(:user_id, user_id) = user_id

Note that, as commented by Liam Sorsby, the most efficient option is likely to check if the parameter is set from your application, and modify the query string accordingly.请注意,正如 Liam Sorsby 所评论的,最有效的选项可能是检查参数是否是从您的应用程序中设置的,并相应地修改查询字符串。 If the parameter is not set, the query does not have a WHERE clause;如果未设置参数,则查询没有WHERE子句; if it is set, then WHERE user_id =:user_id .如果已设置,则WHERE user_id =:user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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