SQL求助

查看 116|回复 9
作者:快手   
[i]
[table][tr][td][table][tr][td]如何通过表1的token查出会员名 然后用会员名去查表2 comid值=1的 就显示这一列的所有
比如 huiyuantoken=tkk 并且comid=5 满足后显示这一行出来

会员名, 这一

demonw   

用子查询,即把表1的结果作为参数在表2查询:
SELECT * FROM jilu WHERE huiyuanming = (SELECT huiyuanming FROM huiyuan WHERE huiyuantoken='tkk') AND comid = 5;
E4E4   

试一下这个吧
SELECT *
FROM huiyuan
WHERE token = 'huiyuantoken'
AND huiyuanming IN (
  SELECT huiyuanming
  FROM jilu
  WHERE comid = 1
)
ANryqq   

以你发的帖子来看,两个表以huiyuanming字段关联,查询如下
[SQL] 纯文本查看 复制代码select t1.huiyuanming ,t1.huiyuantoken ,t1.huiyuanyue ,t2.shoujihao ,t2.comid
from t1 t1 left join t2 t2 on t1.huiyuanming = t2.huiyuanming
where t1.huiyuantoken ='token' and t2.comid ='1'
xx6680   

select * from 表1 t1 LEFT JOIN 表2 t2 on t1.会员名=t2.会员名 where t1.token = 'token' and t2.comid = 1
啊啊唧唧孵唧唧   

-- 两个表的字段 都需要查出来
select j.*, h.* from jilu j
left join huiyuan h on j.id = h.comid
where j.huiyuantoken = ''
;
-- 只需要查询jilu表的字段
select j.*  from jilu j
where
     exists(select 1 from huiyuan h on j.id = h.comid where j.huiyuantoken = ''
)
;
beyondchampion   

SELECT * FROM jilu t2 WHERE t2.huiyuanming IN (SELECT t1.huiyuanming FROM huiyuan t1 WHERE huiyuantoken=tkk) AND t2.comid=5
ZforCoding329   

select h.id,h.huiyuanming,huiyuantoken,shoujihao,comid,neirong from huiyuan h join jilu j on h.huiyuanming=j.huiyuanming where h.huiyuantoken='tkk' and j.comid=5;
jmjackie   

如果数据量大不建议用join,可以用exists
select * from jilu a where exists(select 1 from huiyuan b where a.huiyuanming = b.huiyuanming and b.huiyuantoken = 'token')
Lcy0529   

SELECT t1.huiyuanming, t1.huiyuantoken, t1.huiyuanyue, t2.shoujihao, t2.comid
FROM t1
INNER JOIN t2 ON t1.huiyuanming = t2.huiyuanming AND t2.comid = '1'
WHERE t1.huiyuantoken = 'token';
您需要登录后才可以回帖 登录 | 立即注册

返回顶部