简体   繁体   中英

How to access data using a SQL query that references multiple tables?

I have a few tables and inserted values into them using SQL plus:

INSERT INTO Location(name, addr, phone) 
VALUES ('Texas Branch', '4832 Deercove Drive, Dallas, TX 75208', '214-948-7102');

INSERT INTO Librarian(eid, ID, pay, Loc_name) 
VALUES (2591051, 88564, 30000.00, 'Texas Branch');

INSERT INTO Stored_In(media_id, name) 
VALUES (8733, 'Texas Branch');

Suppose I write a SQL query like select location = "texas branch" , then the output should contain the librarian for the Texas branch and media_id , the name stored_in - in simple words, I should get all the information related to Texas branch)

Can anyone please help write the SQL query?

Something along the lines of:

SELECT * FROM (
    Location
    INNER JOIN 
    Librarian ON Location.name = Librarian.Loc_name
    INNER JOIN 
    Stored_In ON Stored_In.name = Location.name
)
WHERE(
    Location.name='Texas Branch'
)

Because of the SELECT * you will get a bunch of duplicate fields and you should replace this with the proper select.

As others have said, Google JOIN s and understand them.

You need to use joins then the SQL is straightforward. If that is unclear then maybe you need to do some homework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM