帮助连接查询 (MySQL)

发布于 2024-09-12 11:18:35 字数 773 浏览 10 评论 0原文

你好,有人能看出为什么这个查询失败吗?

SELECT  A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, 
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad 
ORDER BY A.featured DESC LIMIT :limit, :offset

但这是有效的:

SELECT *FROM ads 
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset

在第一个中,我不需要表“ads”中的所有列,查询仅返回 ...FROM ads A.

Hello can anybody see why this query fails?

SELECT  A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, 
A.in_dpt, A.description, A.featured FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad 
ORDER BY A.featured DESC LIMIT :limit, :offset

But this one works:

SELECT *FROM ads 
LEFT JOIN featured ON featured.ad = ads.idAd
INNER JOIN dept ON dept.id_dept = ads.in_dpt
INNER JOIN sub_cat_ad ON id_sub_cat = ads.ads_in_Cat
INNER JOIN cat_ad ON idCat_ad = sub_cat_ad.from_cat_ad
ORDER BY featured DESC LIMIT :limit, :offset

In the first one, I don't want all columns from the table "ads", the query returns only columns till ...FROM ads A.

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

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

发布评论

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

评论(1

战皆罪 2024-09-19 11:18:35

如果您指定仅从 A 选择字段,您将得到:仅从 A 中选择字段。
如果您也需要其他表中的字段,则也必须指定它们。

SELECT
  A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
  F.*,
  D.*,
  S.*,
  C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad 
ORDER BY A.featured DESC LIMIT :limit, :offset

If you specify to only select fields from A that's what you get: Only fields from A.
If you want fields from other tables too you have to specify them as well.

SELECT
  A.idAd, A.ads_in_Cat, A.title, A.currency, A.price, A.in_dpt, A.description, A.featured,
  F.*,
  D.*,
  S.*,
  C.*
FROM ads A
LEFT JOIN featured F ON F.ad = A.idAd
INNER JOIN dept D ON D.id_dept = A.in_dpt
INNER JOIN sub_cat_ad S ON S.id_sub_cat = A.ads_in_Cat
INNER JOIN cat_ad C ON C.idCat_ad = S.from_cat_ad 
ORDER BY A.featured DESC LIMIT :limit, :offset
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文