[英]SQL query for 2 different tables
我有桌子:
我正在用php做一個項目,需要針對特定用戶(通過其用戶名)執行SQL查詢-需要在“用戶的旅行歷史記錄”表中了解Trip_Num的Trip_Type。
附上我有的表:
旅行
用戶的旅行記錄
更新:我需要依靠Trip_type中的每個值。 我的意思是我想看“徒步”-4次,“自行車”-3次,這很好嗎? .................................................. select選擇Trips_History。*,Trips.Trip_Type,COUNT(Trip_Type)AS Trip_Type_occurrence來自行程歷史記錄INNER JOIN Trips_History.Trip_Num = Trips.Trip_Num GROUP BY Trip_Type ORDER BY Trip_Type_occurrence DESC –
一個簡單的JOIN
可以做到:
SELECT
th.*,
t.Trip_Type
FROM Trip_History AS th
INNER JOIN Trips AS t
ON t.Trip_Num = th.Trip_Num
嘗試這個:
select Trips_History.*,COUNT(Trips.Trip_Type) AS Trip_Type_occurrence FROM Trips_History INNER JOIN Trips on Trips_History.Trip_Num = Trips.Trip_Num GROUP BY Trip_Type ORDER BY Trip_Type_occurrence DESC
嘗試這個
select t2.traveller_username,t1.Trip_Type from
trips as t1 inner join trips_history as t2 on t1.Trip_num=t2.trip_num
嘗試,
SELECT t.Trip_Type, u.Trip_Num from trips at t
JOIN user_trips_history ON u.Trip_Num = t.Trip_Num
WHERE u.Traveller_Username = 'username';
嘗試做:
SELECT t.Trip_Type, u.Trip_Num FROM trips as t
JOIN user_trips_history as u ON t.Trip_Num=u.Trip_Num
WHERE u.Traveller_Username = 'USERNAME';
你可以試試這個-
SELECT users_trip_history.trip_num, trips.trip_type, COUNT(trips.trip_type) AS trip_occurences
FROM users_trip_history
INNER JOIN trips
ON users_trip_history.trip_num = trips.trip_num
WHERE users_trip_history.traveler_username = "name"
GRUOP BY trip_num;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.