| |

VerySource

 Forgot password?
 Register
Search
View: 32|Reply: 2

Find a master and child table join query, please help, online, etc.

[Copy link]

1

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-2-13 20:00:01
| Show all posts |Read mode
There are two tables:
1. parent table; 2. child table

parent primary key: id
child primary key: id, foreign key p_id

Now you need to query the parent records and sort them. The sorting principle is based on the number of subtables corresponding to each main table record. The main table records with subtable records of 0 must also be displayed.
Reply

Use magic Report

0

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

Post time: 2020-4-17 18:45:02
| Show all posts
select *
from parent as p left outer join child as c
on p.id = c.id
order by c.id asc
Reply

Use magic Report

1

Threads

2

Posts

3

Credits

Newbie

Rank: 1

Credits
3

 China

 Author| Post time: 2020-4-18 10:00:01
| Show all posts
totalentzj:

I think I will describe it clearly. The principle of sorting the main table is to sort by the number of records in the subtable p_id corresponding to the main table ID. E.g:
parent record: id name memo
             1 1 1
             2 2 2
child record: id name memo p_id
             1 c1 c1 1
             2 c2 c2 2
             3 c3 c3 2

The query results are: id name memo
                  2 2 2
                  1 1 1
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list