我猜測,你想從審查的最後一排那麼這樣的事情可能會做( - 音符,如果你已經提供的數據作爲文本而不是圖片,我會提供基於數據的解決方案)
drop table if exists comment;
create table comment(id int,`comment` varchar(10), company varchar(3), state varchar(3), private int);
drop table if exists review;
create table review (id int,avg int,company varchar(3),state varchar(3));
insert into comment values (1,'Test','abc','il',0),(1,'Ver','def','il',0);
insert into review values(1,0,'abc','il'),(2,4,'abc','il'),(3,0,'def','il');
MariaDB [sandbox]> select c.*, r.avg
-> from comment c
-> join review r on c.company = r.company and c.state = r.state
-> where c.`comment` = 'Test' and c.private = 0 and
-> r.id = (select max(r1.id) from review r1 where r1.company = r.company and r1.state = r.state)
-> ;
+------+---------+---------+-------+---------+------+
| id | comment | company | state | private | avg |
+------+---------+---------+-------+---------+------+
| 1 | Test | abc | il | 0 | 4 |
+------+---------+---------+-------+---------+------+
1 row in set (0.00 sec)
請參閱http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-查詢 – Strawberry
平均值您想要的四個審查條目中,選擇它的邏輯是什麼。 –
查閱關於'JOIN'聲明的文檔。這應該可以幫助您從一個表中獲取返回的行。一個簡單的'LEFT JOIN'應該修復它。 https://msdn.microsoft.com/en-us/library/zt8wzxy4.aspx – Cataklysim