[英]how to make inner join with where condition
我有下面的表格,
CREATE TABLE `CreateEvent_tbl` (
`Event_Id` varchar(10) NOT NULL,
`State` varchar(25) DEFAULT NULL,
`District` varchar(35) DEFAULT NULL,
`School` varchar(150) DEFAULT NULL,
`Event_Date` date DEFAULT NULL,
`Created_By` varchar(35) DEFAULT NULL,
`Created_Date_Time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`Event_Id`)
CREATE TABLE `StudentDetails_tbl` (
`Student_Id` varchar(15) NOT NULL,
`Event_Id` varchar(10) DEFAULT NULL,
`Student_Name` varchar(45) DEFAULT NULL,
`Parents_Name` varchar(90) DEFAULT NULL,
`Std_Ph_No` varchar(25) DEFAULT NULL,
`Ph_No_1` varchar(25) DEFAULT NULL,
`Ph_No_2` varchar(25) DEFAULT NULL,
`Email_Id` varchar(50) DEFAULT NULL,
`Address` varchar(250) DEFAULT NULL,
`Created_By` varchar(25) DEFAULT NULL,
`Created_Date_Time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`Student_Id`)
CREATE TABLE `BuyerDetails_tbl` (
`Event_Id` varchar(10) DEFAULT NULL,
`Student_Id` varchar(15) DEFAULT NULL,
`Call_Buy_Id` varchar(10) DEFAULT NULL,
`Buyer_Id` varchar(10) NOT NULL,
`Purchased_Date` date DEFAULT NULL,
`No_Of_Reference` int(11) DEFAULT NULL,
`Created_By` varchar(45) DEFAULT NULL,
`Created_Date_Time` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`Buyer_Id`)
我的查詢是:
select CreateEvent_tbl.Event_Id,CreateEvent_tbl.State,StudentDetails_tbl.Student_Id,StudentDetails_tbl.Student_Name,BuyerDetails_tbl.Purchased_Date
from CreateEvent_tbl
inner join BuyerDetails_tbl on CreateEvent_tbl.Event_Id=BuyerDetails_tbl.Event_Id
inner join StudentDetails_tbl on StudentDetails_tbl.Student_Id=BuyerDetails_tbl.Student_Id
where BuyerDetails_tbl.Buyer_Id="B045";
當我從BuyerDetails_tbl中的Buyer_Id搜索時,顯示StudentDetails_tbl和CreateEvent_tbl中的StudentDetails和EventDetails,其中BuyerDetails_tbl中的Student_Id和Event_Id。
但是上面的查詢不起作用,什么也沒顯示。
我哪里錯了?
注意:我是加入查詢的新手。
由於您正在尋找特定的買家,因此我將以THAT表作為列表中的第一個表,然后再加入其他兩個表。 另外,為了縮短查詢的可讀性,請注意表名稱的“別名”(通過“ ce”,“ sd”和“ bd”)與所有列名稱和聯接的長表名稱。
select
ce.Event_Id,
ce.State,
sd.Student_Id,
sd.Student_Name,
bd.Purchased_Date
from
BuyerDetails_tbl bd
inner join CreateEvent_tbl ce
on bd.Event_Id = ce.Event_Id
inner join StudentDetails_tbl sd
on bd.Student_Id = sd.Student_Id
where
bd.Buyer_Id = "B045";
我將確保您具有基於查詢條件和連接條件的適當索引。
BuyerDetails_tbl-(buyer_id)上的索引以優化您的WHERE標准。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.