Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.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
Mysql或/和查询_Mysql - Fatal编程技术网

Mysql或/和查询

Mysql或/和查询,mysql,Mysql,我用PHP和MySql开发了一个标签系统。网站(表1)与第三个表(表3)中的许多标签(表2)相关联: 表1: ! ----- ! -----------------! ! t1_id ! t1_libelle ! ! ----- ! ---------------- ! ! 1 ! Site 1 ! ! ----- ! ---------------- ! ! 2 ! Site 2 ! 表2: ! t2_id ! tag

我用PHP和MySql开发了一个标签系统。网站(表1)与第三个表(表3)中的许多标签(表2)相关联:

表1:

! ----- ! -----------------!
! t1_id ! t1_libelle       ! 
! ----- ! ---------------- !
! 1     ! Site 1           ! 
! ----- ! ---------------- !
! 2     ! Site 2           ! 
表2:

! t2_id ! tag             ! 
! ----- ! ----------------!
! 3     ! Référencement   ! 
! ----- ! ----------------!
! 4     ! Linking         ! 
! ----- ! ----------------! 
! 5     ! HTML            ! 
表3:

! t1_id ! t2_id     ! 
! ----- ! ----------!
! 1     ! 3         ! 
! ---- ! ---------- !
! 1    ! 4          ! 
! ---- ! ---------- ! 
! 2    ! 4          ! 
! ---- ! ---------- !
! 2    ! 5          ! 
我的查询(如下)显示了带有标签“Référence”或“Linking”的网站,因此显示了网站1和2,但这不是我需要的。我想展示同时具有“Référence”和“Linking”标签的网站(因此这里仅显示“site 1”):

我希望我是清楚的,对不起,我的英语不流利


提前感谢您的帮助。

您需要多次加入表3才能完成所需的内容:

SELECT t1.*
FROM table1 t1
JOIN table3 t3_first
ON t1.id = t3_first.t1_id
AND t3_first.t2_id = 3
JOIN table3 t3_second
ON t1.id = t3_second.t1_id
AND t3_second.t2_id = 4

结果集中只会返回表1中满足两个联接条件的行,我相信这是您想要的。

您需要多次联接到表3才能完成所需的操作:

SELECT t1.*
FROM table1 t1
JOIN table3 t3_first
ON t1.id = t3_first.t1_id
AND t3_first.t2_id = 3
JOIN table3 t3_second
ON t1.id = t3_second.t1_id
AND t3_second.t2_id = 4

结果集中只会返回表1中同时满足两个联接条件的行,我相信这是您想要的。

您可以使用
分组方式和
having
(或其他方法)来执行此操作。更重要的是,学会使用正确的
join
语法。简单规则:在
from
子句中不要使用逗号

select t1.*  -- this is fine, assuming that `id` is unique in `t1`
from table3 t3 join
     table1 t1
     on t3.t1_id = t1.id join
     table2 t2
     on t3.t2_id = t2.id
where t2.tag in ('Référencement', 'Linking')
group by t1.id
having count(*) = 2;

您可以通过
分组方式
拥有
(或其他方法)来实现这一点。更重要的是,要学会使用正确的
join
语法。简单规则:决不要在
from
子句中使用逗号

select t1.*  -- this is fine, assuming that `id` is unique in `t1`
from table3 t3 join
     table1 t1
     on t3.t1_id = t1.id join
     table2 t2
     on t3.t2_id = t2.id
where t2.tag in ('Référencement', 'Linking')
group by t1.id
having count(*) = 2;

您可以使用or,但按“网站”分组,然后计算条件的数量是否与or列表的长度相匹配。此外,使用实际的联接语法更清晰、更清晰,并且被视为标准。您可以使用or,但按“网站”分组“然后计算条件的数量是否与or列表的长度匹配。另外,使用实际的连接语法更清晰、更清晰,并且被认为是标准的。。i、 e.如果可能有两行的标签值为'Linking',那么我们需要类似于
COUNT(DISTINCT t2.tag)=2在更一般的情况下,如果t2中(id,tab)没有唯一的约束。。i、 e.如果可能有两行的标签值为'Linking',那么我们需要类似于<代码>计数(不同的t2.标记)=2