简体   繁体   English

通过联接表从数据库中选择数据

[英]Selecting data from a database via a joined table

I'm currently making a website that is used to advertise car sharing for festivals. 我目前正在制作一个网站,用于宣传节日期间的汽车共享。 I need to list all trips which are currently assigned to a user but seeing as the database relationship would be many to many I have had to make a client_trip table. 我需要列出当前分配给用户的所有行程,但是看到数据库关系将使我不得不创建一个client_trip表的数量很多。

My question: 我的问题:
How would I select trips from the trip table based on the information in my client_trip table? 如何基于client_trip表中的信息从trip表中选择行程?

I'm currently using PostgreSQL and Java servlets. 我目前正在使用PostgreSQL和Java servlet。 Thanks very much for any help. 非常感谢您的帮助。 :) :)

CREATE TABLE users
(
user_id SERIAL,
user_username VARCHAR (20),
user_firstname VARCHAR(20),
user_surname VARCHAR(20),
user_password VARCHAR(50),
user_email VARCHAR(100),
user_role VARCHAR(20),
PRIMARY KEY(user_id)
);

CREATE TABLE trips
(
trip_id SERIAL,
trip_name VARCHAR (100),
trip_user_username VARCHAR (50),
trip_festival_id SERIAL REFERENCES festivals(festival_id),
trip_festival_name VARCHAR(100),
trip_depart_date DATE,
trip_return_date DATE,
trip_spaces INT,
trip_cost Decimal (19,2),
trip_desc VARCHAR,
PRIMARY KEY(trip_id)
);

how would I select trips from the trip table based on the information in my client_trip 我如何根据client_trip中的信息从行程表中选择行程

With a given user_id : 使用给定的user_id

SELECT t.*
FROM   trips t
JOIN   client_trip ct USING (trip_id)
WHERE  ct.user_id = ??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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