简体   繁体   English

使用第二个INNER JOIN或之前的两个表的带有INNER JOIN的SQL

[英]SQL with INNER JOIN for two tables using or before the second INNER JOIN

    select 
    sld.linkid,sld.accept,scd.catid,scd.catname,scd.caturl,scd1.parentcatid
    from sound_link_droos sld
    INNER JOIN sound_cat_droos scd
    ON sld.catid=scd.catid 
OR
    INNER JOIN  sound_cat_droos scd1 
    ON sld.catid=scd1.parentcatid
    WHERE accept = '1' AND scd.catname = '$catname'");

i want to know if i can have or in this statment or ... if the one inner join return fals 我想知道我是否可以拥有或在此陈述中,或者...如果一个内部联接返回错误

sound_cat_droos sound_cat_droos

Catidcatnameparentidaccepturl 1David01http://www.example.com 2Jazz music11http://www.example.com Catidcatnameparentidaccepturl 1David01http://www.example.com 2Jazz音乐11http://www.example.com

sound_link_droos sound_link_droos

linkidlinknamecatidaccepturl 1my love11http://www.example.com/audio.mp3 2my baby 21http://www.example.com/audio.mp3 linkidlinknamecatidaccepturl 1我的爱11http://www.example.com/audio.mp3 2我的孩子21http://www.example.com/audio.mp3

Best solution is to use Union all Instead of OR in Join as given below: 最好的解决方案是使用“全部联合”而不是“连接”中的“ OR”,如下所示:

select sld.linkid,sld.accept,scd.catid,scd.catname,scd.caturl,scd1.parentcatid from sound_link_droos sld
INNER JOIN sound_cat_droos scd
ON sld.catid=scd.catid 
where accept = '1' and scd.catname = '$catname'
union all
select sld.linkid,sld.accept,scd.catid,scd.catname,scd.caturl,scd1.parentcatid from sound_link_droos sld
INNER JOIN  sound_cat_droos scd1 
ON sld.catid=scd1.parentcatid
where accept = '1' and scd1.catname = '$catname';

You can express or logic in the on clause like this: 您可以像这样在on子句中表达or逻辑:

select sld.linkid, sld.accept,s cd.catid, scd.catname, scd.caturl, scd1.parentcatid
from sound_link_droos sld inner join
     sound_cat_droos scd
     on sld.catid = scd.catid or sld.catid = scd.parentcatid 
where accept = 1 and scd.catname = '$catname';

Such joins are usually performance killers, and this might not do what you really want. 这样的联接通常是性能的杀手,这可能无法满足您的实际需求。 If not, ask another question with sample data and desired results. 如果不是,请询问另一个有关样本数据和所需结果的问题。

I removed the single quotes around the '1' , because I'm guessing the value is numeric. 我删除了'1'周围的单引号,因为我猜数值是数字。 Numbers should be compared to numbers. 数字应与数字进行比较。

It is also obligatory to mention parameters. 还必须提及参数。 You should not be munging a query string with parameter values. 您不应该用参数值来修饰查询字符串。 Learn how to properly use parameters. 了解如何正确使用参数。

Without a specification, we can only guess at what the query is supposed to return. 没有规范,我们只能猜测查询应该返回什么。

So here's a guess. 所以这是一个猜测。

We get all _cat_ where catname matches some value: 我们得到所有_cat_ ,其中catname与某个值匹配:

  SELECT scd.catid
       , scd.catname
       , scd.caturl
    FROM sound_cat_droos scd 
   WHERE scd.catname = 'someval' 

We know that a _cat_ could be the parent of other _cat_ . 我们知道_cat_可以是其他_cat_的父_cat_ And we want to also return those children _cat_ . 我们还要返回那些孩子_cat_ Here's a query to do that: 这是执行此操作的查询:

  SELECT scc.catid
       , scc.catname
       , scc.caturl
    FROM sound_cat_droos scc
    JOIN sound_cat_droos scp
      ON scp.catid = scc.parentcatid
   WHERE scp.catname = 'someval'

We can use a UNION ALL set operator to combine the results from those two queries into a single set. 我们可以使用UNION ALL集运算符将来自这两个查询的结果合并为一个集合。 And then we can join that combined set to _link_ 然后,我们可以将该组合集加入_link_

  SELECT sld.linkid
       , sld.accept
       , sca.catid
       , sca.catname
       , sca.caturl
    FROM ( SELECT scd.catid
                , scd.catname
                , scd.caturl
             FROM sound_cat_droos scd 
            WHERE scd.catname = 'someval' 
            UNION ALL 
           SELECT scc.catid
                , scc.catname
                , scc.caturl
             FROM sound_cat_droos scc
             JOIN sound_cat_droos scp
               ON scp.catid = scc.parentcatid
            WHERE scp.catname = 'someval'
         ) sca
    JOIN sound_link_droos sld
      ON sld.catid = sca.catid
     AND sld.accept = '1'

If it's important (for some reason) to know whether the _cat_ we matched was a direct match, or was a match to a child _cat_ , we can include a discriminator column in the inline view query, and return that in the resultset. 如果重要(由于某种原因)要知道我们匹配的_cat_是直接匹配还是与子_cat_匹配,我们可以在内联视图查询中包含一个discriminator列,并将其返回到结果集中。

  SELECT sld.linkid
       , sld.accept
       , sca.catid
       , sca.catname
       , sca.caturl
       , sca.src
    FROM ( SELECT 1 AS src
                , scd.catid
                , scd.catname
                , scd.caturl
             FROM sound_cat_droos scd 
            WHERE scd.catname = 'someval' 
            UNION ALL 
           SELECT 2 AS src
                , scc.catid
                , scc.catname
                , scc.caturl
             FROM sound_cat_droos scc
             JOIN sound_cat_droos scp
               ON scp.catid = scc.parentcatid
            WHERE scp.catname = 'someval'
         ) sca
    JOIN sound_link_droos sld
      ON sld.catid = sca.catid
     AND sld.accept = '1'

We're just guessing at the requirements, at what result should be returned. 我们只是在猜测需求,应该返回什么结果。 And this query might satisfy your specification. 并且此查询可能满足您的规范。 But we don't what that specification is. 但是我们不是那个规范。

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

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