我有一个查询INSERT值INTO表'material_weight'
这里是:
$sql_material = "INSERT INTO `material_weight`
(mat_record_id, material_weight, material_name)
VALUES
((select MAX(record_id) FROM truck_records), '$material_wei_1', '$material_1'),
((select MAX(record_id) FROM truck_records), '$material_wei_2', '$material_2'),
((select MAX(record_id) FROM truck_records), '$material_wei_3', '$material_3'),
((select MAX(record_id) FROM truck_records), '$material_wei_4', '$material_4'),
((select MAX(record_id) FROM truck_records), '$material_wei_5', '$material_5'),
((select MAX(record_id) FROM truck_records), '$material_wei_6', '$material_6'),
((select MAX(record_id) FROM truck_records), '$material_wei_7', '$material_7'),
((select MAX(record_id) FROM truck_records), '$material_wei_8', '$material_8'),
((select MAX(record_id) FROM truck_records), '$material_wei_9', '$material_9'),
((select MAX(record_id) FROM truck_records), '$material_wei_10', '$material_10') ";
我想要一个if语句,IF $ material_wei_1或$ material_wei_2 ...等于零,不要插入此行。
我试着理解IF语法,但没有运气,这就是我试过的......
$sql_material = "INSERT INTO `material_weight`
(mat_record_id, material_weight, material_name)
VALUES
((select MAX(record_id) FROM truck_records), '$material_wei_1', '$material_1'),
if( '$material_wei_2' = 0, ((select MAX(record_id) FROM truck_records), '$material_wei_2', '$material_2') , ),
((select MAX(record_id) FROM truck_records), '$material_wei_3', '$material_3'),...
IF语法有问题吗?
答案 0 :(得分:0)
您将这些值作为参数传递给查询。所以我的第一反应是修复应用程序。
但是,您可以在查询中执行此操作。使用子查询将参数组合在一起,只需在外部查询中使用WHERE
子句。
以下查询执行此操作还会过滤掉重复的子查询,因此这应该比原始版本更快:
INSERT INTO material_weight (mat_record_id, material_weight, material_name)
SELECT mat_record_id, material_weight, material_name
FROM ((SELECT '$material_wei_1' as material_weight, '$material_1' as material_name) UNION ALL
(SELECT '$material_wei_2' as material_weight, '$material_2' as material_name) UNION ALL
. . .
) m CROSS JOIN
(SELECT MAX(record_id) as mat_record_id FROM truck_records) tr
WHERE material_weight > 0;