跨表 SQL 查询的正确语法是什么? [英] What is the proper syntax for a cross-table SQL query?

查看:54
本文介绍了跨表 SQL 查询的正确语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有

SELECT gp_id FROM gp.keywords 
WHERE keyword_id = 15 
AND (SELECT practice_link FROM gp.practices 
     WHERE practice_link IS NOT NULL 
     AND id = gp_id)

这不会提供语法错误,但是对于应该返回行的值,它只返回 0 行.

This does not provide a syntax error, however for values where it should return row(s), it just returns 0 rows.

我想要做的是从 gp.keywords 获取 gp_id,其中关键字表 keyword_id 列是特定值,而 practice_link 是实践表对应于我拥有的 gp_id,它存储在 id 中该表的列.

What I'm trying to do is get the gp_id from gp.keywords where the the keywords table keyword_id column is a specific value and the practice_link is the practices table corresponds to the gp_id that I have, which is stored in the id column of that table.

推荐答案

我什至不确定这是有效的 SQL,所以我很惊讶它竟然能正常工作:

I'm not even sure that is valid SQL, so I'm surprised it is working at all:

SELECT gp_id
FROM gp.keywords
WHERE keyword_id = 15
    AND (SELECT practice_link FROM gp.practices WHERE practice_link IS NOT NULL AND id = gp_id)

这个怎么样:

SELECT kw.gp_id, p.practice_link
FROM gp.keywords AS kw
INNER JOIN gp.practices AS p
    ON p.id = kw.gp_id
WHERE kw.keyword_id = 15

我会像在其他示例中一样避开隐式连接.以后只会流泪.

I would steer clear of implicit joins as in the other examples. It only leads to tears later.

这篇关于跨表 SQL 查询的正确语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆