| |

VerySource

 Forgot password?
 Register
Search
View: 601|Reply: 4

Find a SQL statement

[Copy link]

2

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-8 20:30:02
| Show all posts |Read mode
There are two tables, one is the article table and one is the user table
Article table
id title user1 user2
1 Article 1 1 2

user table
id username
 1 sheet three
 2 Lee Si

Now to get the record as
1 Article 1 Zhang San Li Si
How to construct SQL statements
Reply

Use magic Report

2

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-31 21:00:01
| Show all posts
I asked if I could use subqueries to achieve this SQL statement.
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-4-1 09:30:02
| Show all posts
create table A (id int, title nvarchar (10), user1 int, user2 int)
insert A select 1, 'Article 1', 1, 2

create table B (id int, username nvarchar (10))
insert B select 1, '张三'
union all select 2, '李四'

select A.id, A.title, tmpA.username as user1, tmpB.username as user2 from A
left join B as tmpA on A.user1 = tmpA.id
left join B as tmpB on A.user2 = tmpB.id
--result
id title user1 user2
----------- ---------- ---------- ----------
1 Article 1 Zhang San Li Si

(1 row (s) affected)
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 Invalid IP Address

Post time: 2020-4-1 11:30:01
| Show all posts
Is it OK to connect?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-1 13:00:01
| Show all posts
Select
    wzb.id, wzb.title, yh1.username, yh2.username
From article list wzb
Inner Join user table yh1 on wzb.user1 = yh1.id
Inner Join user table yh2 on wzb.user2 = yh2.id
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