How can I generate the SQL query using SQL::Abstract?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 19:34:06

问题


How do I generate the WHERE clause for this query using SQL::Abstract:

SELECT COUNT(*) FROM table WHERE id = 111 AND NOT FIND_IN_SET(type, '1,2,3,4') AND status = 'pending';

What's the proper way to include conditions like WHERE FIND_IN_SET(type, '1,2,3,4')?


回答1:


See the not_bool unary operator option:

use SQL::Abstract;

my $sql = SQL::Abstract->new;

my $where = {
    id => 111,
    status => 'pending',
    -not_bool => "FIND_IN_SET(type, '1,2,3,4')",
};

my ($query, @bind) = $sql->select( 
    'table',
    'count(*)',
    $where,
);

This is how $query looks:

SELECT count(*) FROM table WHERE ( ( (NOT FIND_IN_SET(type, '1,2,3,4')) 
AND id = ? AND status = ? ) )



回答2:


FIND_IN_SET isn't standard SQL, so SQL::Abstract doesn't have support for it. You can however put any literal SQL into an SQL::Abstract query. I expect your solution lies down that route.




回答3:


Take a look at: DALMP

Database Abstraction Layer for MySQL using PHP

0% fat and extremely easy to use. Only connect to database when needed.

http://code.google.com/p/dalmp/



来源:https://stackoverflow.com/questions/3913659/how-can-i-generate-the-sql-query-using-sqlabstract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!