IN query IN Yii framework parameterized query can only query one solution. yii framework

Source: Internet
Author: User

IN query IN Yii framework parameterized query can only query one solution. yii framework

This example describes how to query only one IN query IN parameter query of Yii framework. We will share this with you for your reference. The details are as follows:

IN the yii framework, when parameterization is used for IN queries, the results are not as expected.

$sql =<<<SQLSELECT id FROM tb WHERE id IN(:ids)SQL;$db = GeneralService::getSlaveDB();$result = $db->createCommand($sql)->query([':ids' => '1013,1015,1017'])->readAll();print_r($result);
Array(  [0] => Array    (      [id] => 1013    ))

So I searched the source code in the yii framework and found that pdo queries were used, so I found the pdo related information and found the cause:A placeholder cannot replace a group of values..

SELECT id FROM tb WHERE userid IN ( ? );

Now that you know the reason, find the alternative method. FIND_IN_SET can meet

$sql =<<<SQLSELECT id FROM tb WHERE FIND_IN_SET(id, :ids)SQL;$db = GeneralService::getSlaveDB();$result = $db->createCommand($sql)->query([':ids' => '1013,1015,1017'])->readAll();print_r($result);
Array(  [0] => Array    (      [id] => 1013    )  [1] => Array    (      [id] => 1015    )  [2] => Array    (      [id] => 1017    ))

FIND_IN_SET function in simple science

FIND_IN_SET(str,strlist)

If the str string is in the strlist consisting of N substrings, the return value ranges from 1 to N.

A string list is a string consisting of substrings separated by commas. If the first parameter is a constant string and the second parameter is the type SET column, the FIND_IN_SET () function is optimized and computed in bits.

If str is not in strlist or strlist is a null string, the return value is 0. If any parameter is NULL, the return value is NULL. This function cannot run normally when the first parameter contains a comma.

[Ps] strlist is a string consisting of commas (,). It cannot be recognized because a space is added to the right of a comma as it is normally used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.