Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取所有用户的SQL语句_Php_Mysql_Sql - Fatal编程技术网

Php 获取所有用户的SQL语句

Php 获取所有用户的SQL语句,php,mysql,sql,Php,Mysql,Sql,我有这个表,我很困惑我如何才能在这个表上生成sql语句,我如何才能得到ALLu_id,在p_id下作为父id 这里是我想要的输出,如果我只想要5作为p_id users usernames p_id 7 miller 5 8 jamie 5 9 honey 7 10 jack 7 收集表格 p_id

我有这个表,我很困惑我如何才能在这个表上生成sql语句,我如何才能得到ALLu_id,在p_id下作为父id

这里是我想要的输出,如果我只想要5作为p_id

users       usernames       p_id
7           miller           5
8           jamie            5
9           honey            7
10          jack             7
收集表格

p_id        u_id

null         1
1            5
1            6
5            7
5            8
7            9
7            10
user_id      user_name

1            millan
2            john
3            max
4            chris
5            jane
6            lester
7            miller
8            jamie
9            Honey
10           Jack
用户表

p_id        u_id

null         1
1            5
1            6
5            7
5            8
7            9
7            10
user_id      user_name

1            millan
2            john
3            max
4            chris
5            jane
6            lester
7            miller
8            jamie
9            Honey
10           Jack
先谢谢你

编辑:

我尝试>=6,但它返回9和10,但6在我的聚集表中从来不是p_id,因此它在我的聚集表中没有u_id


我想得到我的p_id中所有属于它的u_id(p_id)。在我的示例中,5的p_id为7 8 9 10,尽管9和10的p_id为7,但p_id为7,因此它们仍然在5以下

为此,可以使用左连接

SELECT `g`.`p_id`, `u`.* FROM `gather_table` as `g` LEFT JOIN `user_table` as `u` ON `g`.`u_id` = `u`.`user_id` WHERE `g`.`p_id` >= 5
更新:

你可以查一下你的答案

但如果每个u_id只有一个父对象,那么我建议您使用嵌套的集合表结构,这是处理此类问题的更优雅的方式。在这种情况下,表结构如下

CREATE TABLE IF NOT EXISTS `user_table` (
  `u_id` int(11) NOT NULL AUTO_INCREMENT,
  `p_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `lft` int(11) NOT NULL DEFAULT '0',
  `rgt` int(11) NOT NULL DEFAULT '0',
  `level` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`u_id`),
  KEY `idx_left_right` (`lft`,`rgt`)
) DEFAULT CHARSET=utf8;
你可以从这里得到参考。这是Joomla特有的,但可能会有所帮助

select t1.user_id ,t1.user_name,t2.p_id from user as t1 
inner join gather as t2 on t1.user_id = t2.u_id
where t2.p_id >= 5
您想要的输出显示您想要p_id>=5。请尝试对此进行联接查询
有关更多信息,请查看表上的“使用联接”:

SELECT gather_table.p_id, user_table.* FROM gather_table  LEFT JOIN user_table ON gather_table.u_id = user_table.user_id WHERE gather_table.p_id = 5

希望这对你有帮助

select g.u_id,g.p_id,u.user_name from gather_table g,user_table u where g.u_id=u.user_id and g.u_id>=7

你知道吗?我有话要说,如果>=6但6在我的聚集表中没有u\u id,那么它应该返回空结果。好的。是的,我错了。它将返回9,10的记录。根据你的情况,这是正确的。在这种情况下,您想要什么?我需要更改我的用户表吗?你发布的那个?我不知道如何插入level如果>=6怎么办?但是6在我的聚集表中从来都不是p_id,它没有u_id,但是我尝试了你的解决方案,它返回9和10是的,如果你尝试>=6,它将返回9,10,因为7是唯一的p_id,它>=6。那么这有什么错。?