Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.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 获取具有公共父级的所有行_Php_Mysql_Codeigniter 3 - Fatal编程技术网

Php 获取具有公共父级的所有行

Php 获取具有公共父级的所有行,php,mysql,codeigniter-3,Php,Mysql,Codeigniter 3,但它的回应只是: SELECT ta.user_id,ta.interest_parent_id,ta.interest_child_id FROM user_interest ta WHERE ta.user_id=2 AND (SELECT COUNT(*) FROM user_interest tb WHERE ta.interest_parent_id=tb.interest_parent_id

但它的回应只是:

SELECT ta.user_id,ta.interest_parent_id,ta.interest_child_id 
       FROM user_interest ta 
       WHERE ta.user_id=2 AND 
       (SELECT COUNT(*) FROM user_interest tb 
            WHERE ta.interest_parent_id=tb.interest_parent_id
            AND tb.user_id=3 )>1 

任何帮助:我使用带有php/codeigniter的mysql数据库来编写脚本

您可以进行子选择:

SELECT*FROM table,其中名称在SELECT Name FROM table GROUP BY Name中,count*>1 您可以尝试一下:

id parent_id 2 2 欢迎对查询的优化提出任何建议


编辑:

请详细说明您的问题。从你的数据集来看,您希望选择一个具有给定ID的行以及具有该特定父ID的行。我理解正确吗?我有一个表,当我使用ID查询数据时,需要从该表中获取公共值。请对此进行解释。理想情况下,我希望传递两个ID,检查两行,并返回值相等的列。请告诉我是否清楚地理解:您正在传递两个ID,并且仅当它们具有相同的父项时才想要结果?是的,这是正确的。。很抱歉,尚未编辑标题。。我更像一个潜伏者。。我似乎找不到怎么做。。。在本例中,它为id 2提供了一个重复的行。。这是因为我们使用的是并集吗?请看最后两条insert语句。在用户兴趣中插入'VALUES'3','2','2';将用户感兴趣的值“3”、“2”、“1”;`id=3ok的相同父项id两次太好了。。。因此,为了获得计数,我在您建议的查询中运行SELECT DISTINCT,然后将行计数除以2,以获得对应用户id的不同父id的最终数量。。。谢谢!我修改了你问题的标题,使之与上下文相关。
SELECT ta.user_id,ta.interest_parent_id,ta.interest_child_id 
       FROM user_interest ta 
       WHERE ta.user_id=2 AND 
       (SELECT COUNT(*) FROM user_interest tb 
            WHERE ta.interest_parent_id=tb.interest_parent_id
            AND tb.user_id=3 )>1 
id parent_id 2 2
SELECT 
tOne.id,
tOne.parent_id
FROM 
(
    SELECT 
    *
    FROM user_interest A 
    WHERE A.id IN (2,3) 
) tOne
INNER JOIN 

(
    SELECT 
    *
    FROM user_interest A 
    WHERE A.id IN (2,3) 

) tTwo
ON tOne.parent_id = tTwo.parent_id
AND tOne.id <> tTwo.id
ORDER BY tOne.parent_id;