dongyuan1160 2013-08-29 01:10
浏览 16
已采纳

选择表B的所有表A左连接列,其中多个轮胎不会影响显示表A的全部

I have a work order tracking site. On my new_workorder.php script my form is dynamically generated by values in table cl_billing_codes. When the form is submitted, the values go into cl_work_order_billing_codes with the code id, and the quantity and a column with the work order id to tie the values to the work order.

I am in the process of trying to build an edit_workorder.php script. I need to pull all the values of cl_billing_codes while left joining the quantities of the user submitted data from the new_workorder.php form. Here is my best effort.

SELECT
  `cl_billing_codes`.`id`                          AS `id`,
  `cl_billing_codes`.`billing_code`                AS `billing_code`,
  `cl_billing_codes`.`desc`                        AS `desc`,
  `cl_billing_codes`.`point_value`                 AS `point_value`,
  `cl_work_orders_billing_codes`.`quantity`        AS `quantity`,
  `cl_work_orders_billing_codes`.`billing_code_id` AS `id2`
FROM
  `cl_billing_codes`
  LEFT JOIN `cl_work_orders_billing_codes`
      ON `billing_code` = `cl_work_orders_billing_codes`.`billing_code_id`
WHERE
  `cl_billing_codes`.`service` = '1'
  AND `cl_work_orders_billing_codes`.`work_order_id` = 1585
ORDER BY `billing_code`

Doing it this way doesn't work because I need all the data in cl_billing_codes where the service column equals 1, but only the quantity values in the cl_work_order_billing_codes where the work_order_id value in that table equals a defined value and the billing codes match in both tables.

///// Additional /////

Here is a sample of my table data to work with. http://sqlfiddle.com/#!2/49783/1

  • 写回答

1条回答 默认 最新

  • dptt66700 2013-08-29 01:20
    关注

    I think you just need to move the condition on the work_orders_billing_codes table into the on clause. Then if there is no match, you will still get all the appropraite records in billing_codes.

    SELECT bc.`id` AS `id`, bc.`billing_code` AS `billing_code`, bc.`desc` AS `desc`,
           bc.`point_value` AS `point_value`,
           wobc.`quantity` AS `quantity`,
           wobc.`billing_code_id` AS `id2` 
    FROM `cl_billing_codes` bc LEFT JOIN
         `cl_work_orders_billing_codes` wobc
         ON bc.id = wobc.`billing_code_id` AND wobc.`work_order_id` = 1585
    WHERE bc.`service` =  '1' 
    ORDER BY `billing_code`;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器