| |

VerySource

 Forgot password?
 Register
Search
Author: donkey_ngacn

A SQL statement query problem (query minimum value) (urgent)

[Copy link]

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-19 19:15:01
| Show all posts
should be
select a.id, a.name, a.bid, a.aid, a.price from (select a.id, a.name, b.id as bid, b.aid, b.price from A a inner join B b on a.id = b.aid) a, b where a.id = b.aid and a.price <= b.price
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-2-19 20:45:01
| Show all posts
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
result:
ID NAME ID AID PRICE
----------- ---------- ----------- ----------- ------- ----
1 DEMO 3 1 26

(1 row affected)
Reply

Use magic Report

0

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-20 15:30:01
| Show all posts
select a. *, t. * fro a,
(
  select m. * from b m,
  (select aid, min (price) as price from b) n
  where m.aid = n.aid and m.price = n.price
) t
where a.id = t.aid
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-2-20 15:45:02
| Show all posts
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 A.id AS 'A.ID', A.name AS 'A.NAME', MAX (B.id) AS 'B.ID', B.aid as 'B.AID', MIN (B.price) AS 'B.PRICE'
from B LEFT join A on A.id = B.aid
GROUP BY A.id, A.name, B.aid
Reply

Use magic Report

0

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-20 19:30:02
| Show all posts
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. *, t. * from a,
(
  select m. * from b m,
  (select aid, min (price) as price from b group by aid) n
  where m.aid = n.aid and m.price = n.price
) t
where a.id = t.aid

drop table a, b

id name id aid price
----------- ---------- ----------- ----------- ------- ----
1 DEMO 3 1 26

(1 row affected)
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-21 09:15:01
| Show all posts
If there are two data in table B with the same price, why only one data is displayed?
Form B
------------------------------------------
ID AID PRICE
1 1 30
2 1 32
3 1 26
4 1 26
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-2-21 10:00:02
| Show all posts
select M.id AS 'A.ID', M.name AS 'A.NAME',
M.aid as 'B.AID', B.id AS 'B.ID', M.price AS 'B.PRICE' FROM
(select A.id, A.name,
B.aid, MIN (B.price) price
from B LEFT join A on A.id = B.aid
GROUP BY A.id, A.name, B.aid) M LEFT JOIN B ON M.aid = B.aid AND M.price = B.price
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-2-21 15:15:01
| Show all posts
select M.id AS 'A.ID', M.name AS 'A.NAME',
M.aid as 'B.AID', MAX (B.id) AS 'B.ID', M.price AS 'B.PRICE' FROM
(select A.id, A.name,
B.aid, MIN (B.price) price
from B LEFT join A on A.id = B.aid
GROUP BY A.id, A.name, B.aid) M LEFT JOIN B ON M.aid = B.aid AND M.price = B.price
GROUP BY M.id, M.name, M.aid, M.price
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-21 17:15:01
| Show all posts
Still show two records

id name id aid price
----------- ---------- ----------- ----------- ------- ----
1 DEMO 3 1 26
1 DEMO 4 1 26

I just want to show one
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-2-22 02:30:01
| Show all posts
create table A (ID int, Name varchar (10))
insert A select 1, 'DEMO'
union all select 2, 'DEMO2'

create table B (ID int, AID int, PRICE int)
insert B select 1, 1, 30
union all select 2, 1, 32
union all select 3, 1, 26
union all select 4, 1, 26

select * from A
inner join
(
select max (ID) as ID, AID, min (PRICE) as PRICE from B group by AID
) B on A.ID = B.AID

--result
ID Name ID AID PRICE
----------- ---------- ----------- ----------- ------- ----
1 DEMO 4 1 26

(1 row (s) affected)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list