How can I generate the SQL query using SQL::Abstra

2019-07-07 06:33发布

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')?

4条回答
Fickle 薄情
2楼-- · 2019-07-07 06:46

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 = ? ) )
查看更多
趁早两清
3楼-- · 2019-07-07 06:55

This code generates the WHERE clause:

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

my %where = (
    id => 111,
    -nest => \"NOT FIND_IN_SET(type, '1,2,3,4')",
    status => 'pending',
);

my ($stmt, @bind) = $sql->where(\%where, \@order);
查看更多
来,给爷笑一个
4楼-- · 2019-07-07 06:57

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/

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-07-07 07:04

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.

查看更多
登录 后发表回答