相关的 mysql 子查询

发布于 2024-10-12 04:37:53 字数 642 浏览 4 评论 0原文

您好,我想从子查询内的主查询获取变量的值

SELECT t1.*,sq.*,count(distinct(t4.col1)) as count,
FROM t1 LEFT OUTER JOIN( 
                         SELECT t2.col1,
                                t2.col2,
                                t2.col3 
                          FROM t2 
                          WHERE   t2.col1=t1.col1            
                          ORDER BY t2.col2 DESC, t2.col1 DESC
                        ) as sq 
         ON sq.col1=t1.col1  
LEFT OUTER JOIN t3 ON t3.col1=t1.col4 
LEFT OUTER JOIN t4 ON t4.col1=t1.col4 
WHERE t3.col2=x 
GROUP BY t1.col3 
LIMIT 15

当我将其加入同一列时,如何获取子查询 sq 内变量 t1.col1 的值?

HI i want to get the value of the variable from the main query inside the sub query

SELECT t1.*,sq.*,count(distinct(t4.col1)) as count,
FROM t1 LEFT OUTER JOIN( 
                         SELECT t2.col1,
                                t2.col2,
                                t2.col3 
                          FROM t2 
                          WHERE   t2.col1=t1.col1            
                          ORDER BY t2.col2 DESC, t2.col1 DESC
                        ) as sq 
         ON sq.col1=t1.col1  
LEFT OUTER JOIN t3 ON t3.col1=t1.col4 
LEFT OUTER JOIN t4 ON t4.col1=t1.col4 
WHERE t3.col2=x 
GROUP BY t1.col3 
LIMIT 15

How do i get the value of the variable t1.col1 inside the subquery sq when I am joining it on the same column??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2024-10-19 04:37:53

您不能,但以下应该返回与您发布的查询相同的结果。

SELECT  t1.col3            
FROM    t1 
        left outer join ( 
          select  t2.col1
                  ,t2.col2
                  ,t2.col3 
                  ,t2.col4
          FROM    t2 
        ) as sq on sq.col1 = t1.col1 
                   AND sq.col4 = t1.col1
where   t1.col2 = x 
group by 
        t1.col3 
limit 15

注意:您发布的原始查询包含一些语法错误。

  • 选择 * (您的问题)
  • group by order by
  • 使用子选择中的外部选择在子选择中使用

You can't but following should return equivalent results as the query you've posted.

SELECT  t1.col3            
FROM    t1 
        left outer join ( 
          select  t2.col1
                  ,t2.col2
                  ,t2.col3 
                  ,t2.col4
          FROM    t2 
        ) as sq on sq.col1 = t1.col1 
                   AND sq.col4 = t1.col1
where   t1.col2 = x 
group by 
        t1.col3 
limit 15

Note: the original query you've posted contains a few syntax errors.

  • select * with group by
  • order by in a subselect
  • using the outer select in the subselect (your question)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文