| |

VerySource

 Forgot password?
 Register
Search
View: 899|Reply: 6

Joint table query fetch a record

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-14 00:00:01
| Show all posts |Read mode
This is the case, two tables ...

Product table tb_product
p_code (product code) p_status (status)
111 7
222 8
333 9
This table p_code is unique, no duplicates

Usage table tb_usemode
u_code (product code) u_status (status)
111 1
111 1
222 3
222 2
333 0
This table has duplicates in u_code and also in u_status

How to join tables to query:
All unique records with p_status = 8 in tb_product or u_status = 1 in tb_usemode
The product code cannot be repeated.

The result should look like this
p_code u_code p_status u_status
111 111 7 1
222 222 8 2 (3)

If u_status has two, take one of them.

Thank you for your trouble.
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-4-12 13:00:02
| Show all posts
select p_code, u_code, p_status, u_status = min (u_status)
from tb_product a inner join tb_usemode b where a.p_code = b.u_code
group by p_code, u_code, p_status
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-4-12 17:00:01
| Show all posts
select a.p_code, b.u_code, a.p_status, b.u_status from table1 a join table2 b on a.p_code = b.u_code where b.u_status = 1 group by .p_code, b.u_code, a.p_status
union all
select a.p_code, b.u_code, a.p_status, b.u_status from table1 a join table2 b on a.p_code = b.u_code where a.p_status = 8 and b.u_status in (select top 1 u_status from table2) group by .p_code, b.u_code, a.p_status
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-4-12 17:30:01
| Show all posts
declare @tb_product table (p_code int, p_status int)
insert @tb_product
select 111, 7
union all select 222, 8
union all select 333, 9


declare @tb_usemode table (u_code int, u_status int)
insert @tb_usemode
select 111, 1
union all select 111, 1
union all select 222, 3
union all select 222, 2
union all select 333, 0

select p_code, u_code, p_status, u_status = min (u_status)
from @tb_product a inner join @tb_usemode b on a.p_code = b.u_code--where where is changed to on
group by p_code, u_code, p_status


(The number of rows affected is 3)


(The number of rows affected is 5)

p_code u_code p_status u_status
----------- ----------- ----------- -----------
111 111 7 1
222 222 8 2
333 333 9 0

(The number of rows affected is 3)
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-4-12 18:30:01
| Show all posts
declare @tb_product table (p_code int, p_status int)
insert @tb_product
select 111, 7
union all select 222, 8
union all select 333, 9


declare @tb_usemode table (u_code int, u_status int)
insert @tb_usemode
select 111, 1
union all select 111, 1
union all select 222, 3
union all select 222, 2
union all select 333, 0

select p_code, u_code, p_status, u_status = min (u_status)
from @tb_product a inner join @tb_usemode b on a.p_code = b.u_code--where where is changed to on
where u_status> 0--Exclude the landlord from showing that this record is 0
group by p_code, u_code, p_status


(The number of rows affected is 3)


(The number of rows affected is 5)

p_code u_code p_status u_status
----------- ----------- ----------- -----------
111 111 7 1
222 222 8 2

(The number of rows affected is 2)
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-13 09:00:01
| Show all posts
Ok

select p_code, u_code, p_status, u_status = min (u_status)
from tb_product a
inner join tb_usemode b on a.p_code = b.u_code
where a.p_status = 8 or b.u_status = 1
group by p_code, u_code, p_status

Thank you.
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

 Author| Post time: 2020-4-13 10:30:02
| Show all posts
Thank you all.
I changed it ...

select p_code, u_code, p_status, u_status = min (u_status)
from tb_product a
inner join tb_usemode b on a.p_code = b.u_code
where a.p_status = 8 or b.u_status = 1
group by p_code, u_code, p_status

Yes, thank you again.
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