在我的服务器中,我有几个查询调用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
)时,我必须在我的代码中单独编辑每个查询。这需要一段时间,并不理想!
制作可应用于所有查询的验证功能的最佳方法是什么?我想我可以制作一个$_GLOBAL
$validation_sql='AND reversed!=1 AND amount>0 AND ip IS NOT NULL'
,但我也认为,也许创建自定义MYSQL功能会更好(如果可以做到的话)。
你们觉得怎么样?最好的方法是什么?
答案 0 :(得分: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