简体   繁体   English

如何与where条件进行内部连接

[英]how to make inner join with where condition

I have tables like below, 我有下面的表格,

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`)

My query is : 我的查询是:

      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";

when i search by Buyer_Id from BuyerDetails_tbl, display StudentDetails and EventDetails from StudentDetails_tbl and CreateEvent_tbl where Student_Id and Event_Id in BuyerDetails_tbl. 当我从BuyerDetails_tbl中的Buyer_Id搜索时,显示StudentDetails_tbl和CreateEvent_tbl中的StudentDetails和EventDetails,其中BuyerDetails_tbl中的Student_Id和Event_Id。

But above query not working,shows nothing. 但是上面的查询不起作用,什么也没显示。

where i am wrong? 我哪里错了?

Note : i am new for joining queries. 注意:我是加入查询的新手。

Since you are looking for a specific buyer, I would start with THAT table as the first in the list, then join to the other two. 由于您正在寻找特定的买家,因此我将以THAT表作为列表中的第一个表,然后再加入其他两个表。 Also, to shorten readability of the query, notice the "aliasing" of the table names (via "ce", "sd" and "bd" ) vs the long table names for all the column names and joins. 另外,为了缩短查询的可读性,请注意表名称的“别名”(通过“ 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";

I would ensure you have proper indexes based on the query criteria and join conditions.. 我将确保您具有基于查询条件和连接条件的适当索引。

BuyerDetails_tbl -- index on (buyer_id) to optimize your WHERE criteria. BuyerDetails_tbl-(buyer_id)上的索引以优化您的WHERE标准。

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

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