如何使用case语句来确定要连接的表

时间:2013-03-11 08:09:52

标签: mysql

如何使用case语句来确定左边哪个表连接到这个sql语句:

SELECT c.dentist_id,
       c.pack_id,
       c.check_id,
       sql1.vendor_no ,
       p.pack_trans_id,
       r.lot_no,
       IF(ml.payment_type='Prepay','Pre-Paying','Post-Paying') payment_type,
       ml.image_path,
       ml.metal_id,
       tp.total_paid_refiner,
       sql1.percentage,
       sql1.contact_percentage,
       sql1.agent_percentage,
       c.agent_dentist,
       c.check_amt,
       c.check_image,
       c.check_date_sent,
       c.check_no,
       c.check_clear_date,
       ml.spot_gold_price,
       ml.total_weight,
       ml.estimated_price,
       ml.expect_more,
       ml.how_long_acquire,
       sa.full_name,
       pd.company_name,
       pd.phone_no,
       pd.email
FROM tbl_check AS c
LEFT JOIN tbl_pack_list AS p ON p.pack_id=c.pack_id
LEFT JOIN tbl_pack_list_details AS pd ON pd.pack_id=c.pack_id
LEFT JOIN
  (SELECT vendor_no,
          paying_contact,
          contact_percentage,
          percentage,
          pay_to,
          agent_percentage,
          dentist_id did2,
          sales_agent_id
   FROM tbl_mst_dentist
   GROUP BY dentist_id) sql1 ON sql1.did2=c.dentist_id
LEFT JOIN tbl_refining r ON r.pack_id=c.pack_id
LEFT JOIN tbl_metals_list ml ON ml.pack_id=c.pack_id
LEFT JOIN tbl_payment tp ON tp.pack_id=c.pack_id
LEFT JOIN tbl_sales_agent sa ON sa.sales_agent_id=sql1.sales_agent_id
LEFT JOIN
  (SELECT * CASE c.agent_dentist WHEN 'a' THEN
     (SELECT sa.* ,
             sm.state_code
      FROM tbl_sales_agent AS sa,
           tbl_mst_state AS sm
      WHERE sa.sales_agent_id = c.sale_agent_id
        AND sa.state=sm.state_name) ELSE
     (SELECT *
      FROM tbl_mst_dentist
      WHERE dentist_id = c.dentist_id))
WHERE 1
  AND c.paying_percent !=0
ORDER BY p.pack_trans_id DESC

0 个答案:

没有答案