获取多个连接中的行数

时间:2014-12-05 08:20:56

标签: mysql sql select join group-by

我有4张桌子

1)comp_login

id          com_name                            co_logo           
------  ----------------------------------  ------------------
 1  UNFAN                               default_logo.png  
 2  T&P                                 default_logo.png  
 3  Elliot                              default_logo.png  
 117  walkin                              default_logo.png  
118  12 starShipping Management          default_logo.png  
119  ABS Marine                          default_logo.png  
120  Accord Marine Management            default_logo.png  
121  Admiral Marine                      default_logo.png  
122  AET                                 default_logo.png  
123  Air Borne Rec.Pvt.Ltd.              default_logo.png  
125  Apex Marine                         default_logo.png  

2)m_medicine

 Med_Id  G_Name                                B_Name                                                      
------  -------------------------------  ---------------------
   1  Acetylsalicylic acid                  Aspirin, aspro                                                     
   2  Aciclovir                             Zovirax, acyclovir                                                 
   3  Amoxicillin + clavulanate             Augmentin, co-amoxcylav                                            
   4  Artemether + Lumifantrine             Riamet                                                             
   5  Azithromycin                          Zithromax                                                          
   6  Cetirizine                            Zyrtec                                                             
   7  Ciprofloxacin                         Ciproxin , Cipro                                                   
   8  Dizepam                               Valium                                                             
   9  Docusate with senna (for equivalent)  Coloxi                                                             
  10  Doxycyline                            Neo-Dagracyline, Unodox, 

3)m_shipinv

    Id   MedId  ShipId  
------  ------  --------
 1       1      2918
 4       2      2918
 5       3      2918
 6       4      2918
 7       5      2918
 9       6      2918
10       7      2918
11       8      3095           

4)tbl_vesselmaster

    id   co_id  vessel_name                
------  ------  ---------------------------
 2918     123  Sima Sadaf   
 2930     123  Aegean Odyassey            
 2925     123  Apj Jad                    
 2923     123  Astor                      
 2924     123  Astro                      
 2941     123  Discovery                  
 2937     123  Disney  Fantasy            
 2919     123  Disney Cruise              
 2940     123  Disney Dream               
 2933     123  Disney Magic               
 2934     123  Disney Wonder 
 3095     125  sdare     

我希望它只展示公司拥有的船只......我只想展示司马萨福...因为一家公司有很多船只而司马萨达夫是公司的船只123.所以只有船只那么应该显示公司所有者。船舶sdare不应显示,因为它不是公司的一部分

Vessel_ID   Vessel_Name   Total_Medicine
----------   ----------   ----------------
 2918         Sima Sadaf        7

2 个答案:

答案 0 :(得分:1)

下面应该开始吧。我没有在查询中看到药物名称的相关性。我还假设comp_login id对应于容器id。我不确定它在架构中的相关性如何。以下是查询的良好起点。

SELECT
    vs.vessel_id,
    vs.vessel_name,
    COUNT(m.id)
FROM
    `t_vesselmaster` vs
    INNER JOIN `m_shipinv` m
        ON vs.id = m.ShipId
    INNER JOIN `comp_login` cl
        ON vs.id = cl.id
GROUP BY vs.vessel_id, vs.vessel_name

答案 1 :(得分:0)

试试这个:

SELECT vs.id as Vessel_ID, vs.vessel_name, COUNT(DISTINCT m.id) Total_Medicine
FROM t_vesselmaster vs
INNER JOIN m_shipinv m ON vs.id = m.ShipId
INNER JOIN comp_login cl ON vs.co_id = cl.id
WHERE vs.co_id = 123
GROUP BY vs.vessel_id, vs.vessel_name;