简体   繁体   English

SQL选择里面选择

[英]SQL select inside select

I have 3 tables. 我有3张桌子。

person{personid,name, etc}
bid{bidid,personid,etc}
rating{ratingid,bidid,rating}

A person gets the rating after the bid is accepted by customers. 投标被客户接受后,一个人将获得评级。 So 1 bid = 1 rating . 因此, 1 bid = 1 rating And then the person bids another order, but the rating won't show up. 然后该人出价另一个订单,但不会显示等级。

I already tried: 我已经尝试过:

SELECT a.namausaha,ROUND(AVG(c.rating)) AS rating,a.kota,a.kontak,b.bidprice,a.mitraid
FROM tb_mitra a
JOIN tb_bid b ON b.mitraid=a.mitraid
LEFT JOIN tb_rating c ON c.bidid=b.bidid
WHERE b.orderid='OD004' AND b.statusbidid='1'
GROUP BY a.mitraid  

but it doesn't work. 但它不起作用。

How to do it? 怎么做? I want to show the rating for every person. 我想显示每个人的评价。

在此输入图像描述

Some rating return null due to left join 由于left join某些rating返回null

Try this 尝试这个

SELECT a.name,
  ROUND(AVG(CASE WHEN c.rating IS NULL THEN 1 ELSE c.rating END )) AS rating,
  a.etc,b.etc,a.personid
FROM person a
JOIN bid b ON b.personid=a.personid
LEFT JOIN rating c ON c.bidid=b.bidid
GROUP BY a.personid 

SQL Fiddle SQL小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM