两表关联查询,怎么写哦?
news表存着一些商家动态信息(新闻活动信息)
base表为商家的基本信息(名称、类型等等)
news 字段
id title indexuid ..
base 字段
id type indexuid ..
这两表中indexuid 是互通的
现在就是要在news表中查询20条记录,但是商家类型为指定的,但是news表并没有这样的字段记录商家类型,必须联动base表查询
我是这样的写的:
select top 20 * from news where ck=1 as a where id=(select max(id) from base as b where b.indexuid=a.indexuid and b.type=10)
这样写提示报错了 在as a 附近
求高手指点一下
[解决办法]
按你的方式:
select top 20 * from news as a where ck=1 where id=(select max(id) from base as b where b.indexuid=a.indexuid and b.type=10)
SELECT TOP 20 a.*
FROM news AS a
LEFT OUTER JOIN base AS b
ON b.indexuid=a.indexuid
where a.ck=1 AND b.type=10