Priyamanu
Priyamanu

Reputation: 183

WHERE OR query with WHERE AND with PHP-MySQLi-Database-Class

I am using MysqlDb Class in my Project.

$fromquery = $jdb->where("ac_ag_Id",1)->orWhere("ac_ag_Id",2)->orWhere("ac_ag_Id",4)->orWhere("ac_ag_Id",17)->where("c_Id",$cc_Id)->get("tbl_accounts")

This is my current query and the result is

SELECT  * FROM tbl_accounts WHERE  ac_ag_Id = '1'  OR ac_ag_Id = '2'  OR ac_ag_Id = '4'  OR ac_ag_Id = '17'  AND c_Id = '2'

Here, c_Id must be 2, but I am getting result of c_Id of 1 also. So I added a bracket which looks like this and I get the currect result

SELECT  * FROM tbl_accounts WHERE  (ac_ag_Id = '1'  OR ac_ag_Id = '2'  OR ac_ag_Id = '4'  OR ac_ag_Id = '17')  AND c_Id = '2' 

How to get this result form that class like adding function andWhere or something?

Upvotes: 0

Views: 1357

Answers (2)

Paul Spiegel
Paul Spiegel

Reputation: 31812

Like Vipin Jain suggested, you can use IN instead of many similar ORs. The syntax for MysqliDb Class would be:

$fromquery = $jdb->where("c_Id",$cc_Id)
                 ->where("ac_ag_Id", [1,2,4,17], 'IN')
                 ->get("tbl_accounts");

Upvotes: 1

Vipin Jain
Vipin Jain

Reputation: 3756

You should use IN clause instead of write multiple OR operator

SELECT  * FROM tbl_accounts WHERE  ac_ag_Id IN(1,2,4,17);

using IN clause you get all record which ac_ag_Id in (1,2,4,17).

for more about IN clause link. above query run on mysql. i am not php developer

Upvotes: 2

Related Questions