这个SQL有什么问题吗?

发布于 2024-09-10 14:23:45 字数 858 浏览 5 评论 0原文

我确信这是我忽略的非常简单的事情,但 MS SQL 对我来说是新的——不过我(或者至少认为我是)对基本的 MySql 相当满意。

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17,   dl.day_19 
FROM lnktrk_links AS l, lnktrk_hourly AS h, lnktrk_daily AS dl 
LEFT JOIN lnktrk_descriptions AS d ON l.link_id = d.link_id 
WHERE l.link_id = h.link_id AND l.link_id = dl.link_id AND l.link_is_click = 1

我得到的错误是:

'The multi-part identifier "l.link_id" could not be bound.

但是 l.link_id 肯定存在。以下没有连接的查询按预期工作:

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17, dl.day_19 
FROM lnktrk_links AS l, lnktrk_hourly AS h, lnktrk_daily AS dl, lnktrk_descriptions AS d 
WHERE l.link_id = h.link_id AND l.link_id = dl.link_id AND d.link_id = l.link_id AND l.link_is_click = 1

I'm sure this is something really simple that I'm overlooking, but MS SQL is new to me -- I am (or at least thought I was) fairly comfortable with basic MySql though.

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17,   dl.day_19 
FROM lnktrk_links AS l, lnktrk_hourly AS h, lnktrk_daily AS dl 
LEFT JOIN lnktrk_descriptions AS d ON l.link_id = d.link_id 
WHERE l.link_id = h.link_id AND l.link_id = dl.link_id AND l.link_is_click = 1

The error I get is:

'The multi-part identifier "l.link_id" could not be bound.

However l.link_id definitely exists. The following query without the join works as expected:

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17, dl.day_19 
FROM lnktrk_links AS l, lnktrk_hourly AS h, lnktrk_daily AS dl, lnktrk_descriptions AS d 
WHERE l.link_id = h.link_id AND l.link_id = dl.link_id AND d.link_id = l.link_id AND l.link_is_click = 1

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

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

发布评论

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

评论(4

记忆消瘦 2024-09-17 14:23:45

试试这个:

SELECT 
l.link_id, 
l.link_allcount, 
d.desc_id, 
d.desc_count, 
d.desc_text, 
h.hour_17,   
dl.day_19 

FROM lnktrk_links AS l
inner join lnktrk_hourly AS h
on l.link_id = h.link_id
inner join  lnktrk_daily AS dl
on l.link_id = dl.link_id 
LEFT JOIN lnktrk_descriptions AS d ON l.link_id = d.link_id 
WHERE   l.link_is_click = 1

try this :

SELECT 
l.link_id, 
l.link_allcount, 
d.desc_id, 
d.desc_count, 
d.desc_text, 
h.hour_17,   
dl.day_19 

FROM lnktrk_links AS l
inner join lnktrk_hourly AS h
on l.link_id = h.link_id
inner join  lnktrk_daily AS dl
on l.link_id = dl.link_id 
LEFT JOIN lnktrk_descriptions AS d ON l.link_id = d.link_id 
WHERE   l.link_is_click = 1
梦明 2024-09-17 14:23:45

我认为语法

lnktrk_daily AS dl 
LEFT JOIN lnktrk_descriptions AS d 
    ON l.link_id = d.link_id 

比逗号分隔的子句绑定得更紧密,因此该部分中没有 l

您所拥有的内容分组为:

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17,   dl.day_19 
FROM lnktrk_links AS l, lnktrk_hourly AS h, 
( lnktrk_daily AS dl 
  LEFT JOIN lnktrk_descriptions AS d 
  ON l.link_id = d.link_id )
WHERE l.link_id = h.link_id AND l.link_id = dl.link_id AND l.link_is_click = 1

I think it is that the syntax binds

lnktrk_daily AS dl 
LEFT JOIN lnktrk_descriptions AS d 
    ON l.link_id = d.link_id 

more tightly than the comma-separated clauses so you don't have an l in that part

What you have is grouped as:

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17,   dl.day_19 
FROM lnktrk_links AS l, lnktrk_hourly AS h, 
( lnktrk_daily AS dl 
  LEFT JOIN lnktrk_descriptions AS d 
  ON l.link_id = d.link_id )
WHERE l.link_id = h.link_id AND l.link_id = dl.link_id AND l.link_is_click = 1
绻影浮沉 2024-09-17 14:23:45

LEFT JOIN正在将lnktrk_daily AS dllnktrk_descriptions AS d连接起来 - 这些都不被称为l,所以< em>在JOINON子句的上下文中,l没有任何意义。

看起来目前您正在使用“交叉连接加 WHERE 子句”样式,而不是“带有 ON 子句的内连接”样式。如果您切换到后者,那么 l 应该在整个 FROM 子句中有意义。

That LEFT JOIN is joining lnktrk_daily AS dl with lnktrk_descriptions AS d - neither of these are called l, so in the context of that JOINs ON clause, l doesn't mean anything.

It looks like at the moment you are using the 'cross join plus WHERE clauses' style rather than 'inner joins with ON clauses' style. If you switch to the latter then l should have meaning across the whole FROM clause.

深巷少女 2024-09-17 14:23:45

你应该加入它们 - 而不是使用 FROM

试试这个

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17,   dl.day_19 
FROM lnktrk_links AS l
LEFT JOIN lnktrk_hourly AS h ON l.link_id = h.link_id
LEFT JOIN lnktrk_daily AS dl ON l.link_id = dl.link_id
LEFT JOIN lnktrk_descriptions AS d ON l.link_id = d.link_id 
WHERE l.link_is_click = 1

You should join them all - instead of using FROM

Try this

SELECT l.link_id, l.link_allcount, d.desc_id, d.desc_count, d.desc_text, h.hour_17,   dl.day_19 
FROM lnktrk_links AS l
LEFT JOIN lnktrk_hourly AS h ON l.link_id = h.link_id
LEFT JOIN lnktrk_daily AS dl ON l.link_id = dl.link_id
LEFT JOIN lnktrk_descriptions AS d ON l.link_id = d.link_id 
WHERE l.link_is_click = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文