一个SQL语句查询问题(查询最小值

两个表A,B
A有ID,NAME等字段
B有ID,AID,PRICE等字段
注:A.ID关联B.AID

我想按A分类查询B产品的最低价?

假如有数据:
A表
------------
ID NAME
1 DEMO
2 DEMO2
------------------------

B表
-------------
ID AID PRICE
1 1 30
2 1 32
3 1 26
-------------
如何查询到以下结果
---------------
A.ID A.NAME B.ID B.AID B.PRICE
1 DEMO 3 1 26

1、-------------------

select A.ID, A.NAME, B.ID , B.AID, B.PRICE from tablea a join tableb b on a.id=b.aid where b.price in(select min(price) from tableb group by aid)

2、--------------------

create table A(id int, name varchar(10))

insert into a(ID,NAME)
select 1,'DEMO'
union all
select 2,'DEMO2'
select * from a

create table B(id int , aid int ,price int)

insert into b(id,aid,price)
select 1,1,30
union all
select 2,1,32
union all
select 3,1,26

---
select distinct *
from a,b
where a.id = (select max(id) from a) and b.id = (select max(id) from b)

3、----------------------

select distinct *
from a,b
where a.id = b.aid and b.price = (select min(price) from b)

4、-----------------------

create table A(id int, name varchar(10))

insert into a(ID,NAME)
select 1,'DEMO'
union all
select 2,'DEMO2'


create table B(id int ,aid int ,price int)

insert into b(id,aid ,price)
select 1,1,30
union all
select 2,1,32
union all
select 3,1,26

select A.ID, A.NAME, B.ID , B.AID, B.PRICE from a a
join b b on a.id=b.aid
where b.price in(select min(price) from b b group by b.aid)
drop table a
drop table b
结果:
ID NAME ID AID PRICE
----------- ---------- ----------- ----------- -----------
1 DEMO 3 1 26

(所影响的行数为 1 行)

你可能感兴趣的:(sql语句)