2012-12-28 10 views
5

私は、次の表持つ条件mysqlでselectで内部結合を使う方法は?

を以下にmysqlデータベースからレコードを取得したい:

  • ORGANIZATION_表

    organizationId | organizationname 
    
  • キャンペーンの表

    camp_id | camp_name | adv_id | organizationId 
    
  • を広告SEの表

    adv_id | adv_name 
    
  • ad_display表私はこのようなレコードを取得したいad_displayテーブルから今

    ad_displayId | adv_id | camp_id | 
    

:どこorganizationId ==としてWHERE Condtionで

| adv_name | camp_name | organizationname 

基本的に、私はcampaign_id =のキャンペーンの広告リストを希望しますか?

注:たぶんad_display

答えて

7

これを試してみてください:

SELECT ad.ad_displayId, a.adv_name, c.camp_name, o.organizationname 
FROM ad_display ad 
INNER JOIN advertise a ON ad.adv_id = a.adv_id 
INNER JOIN campaign c ON ad.camp_id = c.camp_id 
INNER JOIN organization o ON c.organizationId = o.organizationId 
WHERE o.organizationId = ?; 
+0

のthnxの下に...そのはworked..actuallyこのmysqlのに新しいですし、また、SQLクエリのために...しかし、すべての人は、 quick..tnxこのため –

2

に複数のエントリにcamapignテーブルと同じcamp_idにこのような何か同じadv_idに対して複数のエントリがあるはず?

SELECT Distinct 
    advertise.adv_name, 
    campaign.camp_name, 
    organization_.organizationname 
FROM 
    advertise inner join campaign 
    on advertise.adv_id = campaign.adv_id 
    inner join organization_ 
    on campaign.organizationID = organization_.organizationID 
WHERE 
    organization_.organizationID = 1 
1
SELECT * FROM campaign 
LEFT JOIN ad_display ON campaign.camp_id = ad_display.camp_id 
LEFT JOIN advertise ON ad_display.adv_id = advertise.adv_id 
WHERE campaign.organizationId = <?> 
1

利用コード

SELECT dv_name, 
     camp_name, 
     organizationname 
FROM organization 
     inner join campaign 
       ON organization.organizationid == campaign.organizationid 
     left join advertise 
       ON campaign.adv_id == advertise.adv_id 
WHERE condition =? ; 
1
SELECT advertise.adv_name, 
     campaign.camp_name, 
     organization_Table.organizationname 
FROM ad_display 
    INNER JOIN advertise ON ad_display.adv_id = advertise.adv_id 
    INNER JOIN campaign ON ad_display.camp_id = campaign.camp_id 
    INNER JOIN organization_ Table ON campaign.organizationId = organization_Table.organizationId 
    AND organization_Table.organizationId=? 
-1
SELECT batch_number, 
     Count(receipt_number) AS tot_rec, 
     Sum(amount)   AS tot_amount 
FROM receipt_header 
GROUP BY batch_number 
UNION ALL 
SELECT batch_number, 
     Count(receipt_lines.receipt_number), 
     Sum(receipt_lines.amount) 
FROM receipt_lines 
     INNER JOIN receipt_header 
       ON receipt_header.record_id = receipt_lines.header_record_id 
GROUP BY batch_number 
ORDER BY batch_number ASC; 
関連する問題