0

在我的整个服务器中,我有几个查询调用leads使用某些条件来验证它们的表。

SELECT * FROM leads WHERE user_id=3 //This line changes depending on the data needed
AND reversed!=1 AND amount>0 AND ip IS NOT NULL //These 'validation' statements are always there.

SELECT * FROM leads WHERE user_id=10 AND special_case=1
AND reversed!=1 AND amount>0 AND ip IS NOT NULL

SELECT * FROM leads WHERE user_id=15  AND special_case2=1 
AND reversed!=1 AND amount>0 AND ip IS NOT NULL

这些查询的第二行表示几乎每个查询中都存在的验证子句。问题是,当我需要添加另一个验证语句(如AND another_column=1)时,我必须在我的代码中单独编辑每个查询。这需要一段时间,并不理想!

制作可应用于所有查询的验证功能的最佳方法是什么?我在想我可以做一个$_GLOBALlike $validation_sql='AND reversed!=1 AND amount>0 AND ip IS NOT NULL',但我也认为,也许创建一个自定义 MYSQL 函数会更好(如果这甚至可以做到的话)。

你们有什么感想?最好的方法是什么?

4

1 回答 1

0

做一个视图,valid_leads

CREATE VIEW valid_leads AS
SELECT *
FROM leads
WHERE reversed!=1 AND amount>0 AND ip IS NOT NULL

然后你可以这样做:

SELECT *
FROM valid_leads
WHERE user_id = 15 AND special_case2 = 1
于 2013-04-11T22:46:43.057 回答