Ask the PHP array as the MySQL query condition where xx in array, how to handle

Source: Internet
Author: User
Tags mysql query
Originally think of the array $customerid split out, to determine the length of different variables, the number of variables to be judged, and then write the bottom of the MySQL query, some trouble, ask there is no good way, thanks!

---------------------------------------------
$customerid:

Array (size=2)
0 = String ' 5 ' (length=1)
1 = String ' + ' (length=2)
----------------------------------------------

$customer->where ("id in". $customerid)->count (); That's the point, of course, it's not right to put the array directly here.


Reply to discussion (solution)

Because you want to count the MySQL statement below, it's best to have one. It is also not good to loop a query in one line.

foreach ($customerid as & $value) {
$count = $customer->where ("id in".) ($value) ")->count ();
$arrcount [] = $count;
}

$count = 0;
foreach ($arrcount as & $value) {
$count = $count + $value;
}

It's settled, and the new problem is coming.

How to control, different conditions, (show Top 10 records)
For example
WHERE ("ID in". (3)) there are 2
WHERE ("ID in". (2)) there are 1

Actually, what products are collected by each user. One user may have more than one collection.
I want to show them the top 10 summary, with a joint query, do not know how many times, and then cycle count, good trouble.
Is the best way to change the table structure?

In the Product table, add a userid? A user can collect different products? It seems that the design can

The first question, if it is to check the single ID, the direct id = XX can be, no need to use in
If you want to check multiple IDs, combine the array with the IMPLODE function into the ID in (1, 2, 3) string of this format
The last question, if your requirement is to output a list of users, and output each of the top 10 favorites, you should consider caching




Every cool
  • 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.