从数据库分组用户

时间:2017-11-10 09:02:16

标签: php mysql mysqli mysql-error-1064

我有一张这样的表

+--------+----------+--------------+------------------+
| id     | user_id  | preference_id|Sub_Preference_Id |
+--------+----------+--------------+------------------+
|  15145 | 55       |    2         |      7           |
|  15146 | 56       |    2         |      7           |
|  15148 | 58       |    3         |      10          |
|  15150 | 52       |    3         |      12          |
|  15314 | 59       |    1         |      1           |
|  15315 | 60       |    3         |      12          |
|  15316 | 57       |    3         |      12          |
+--------+----------+--------------+------------------+

我想首先基于相同的preference_id对成员进行分组,然后在该特定的相同preference_id中再次我想要对相同的Sub_Preference_Id进行分组。

意味着,我想要一个这样的团体:

+--------+----------+--------------+------------------+
| id     | user_id  | preference_id|Sub_Preference_Id |
+--------+----------+--------------+------------------+
|  15145 | 55       |    2         |      7           |
|  15146 | 56       |    2         |      7           |
+--------+----------+--------------+------------------+

+--------+----------+--------------+------------------+
| id     | user_id  | preference_id|Sub_Preference_Id |
+--------+----------+--------------+------------------+
|  15150 | 52       |    3         |      12          |
|  15315 | 60       |    3         |      12          |
|  15316 | 57       |    3         |      12          |
+--------+----------+--------------+------------------+

+--------+----------+--------------+------------------+
| id     | user_id  | preference_id|Sub_Preference_Id |
+--------+----------+--------------+------------------+
|  15314 | 59       |    1         |      1           |
+--------+----------+--------------+------------------+

+--------+----------+--------------+------------------+
| id     | user_id  | preference_id|Sub_Preference_Id |
+--------+----------+--------------+------------------+
|  15148 | 58       |    3         |      10          |
+--------+----------+--------------+------------------+

现在我这样做了:

$preference_idss = array('1','2','3','4','5','6');

foreach ($preference_idss as $preference_ids) {

    $query ="SELECT * FROM gic_user_preference where preference_id='$preference_ids'";

    $result_preferencea = mysqli_query($createCon->connect(), $query);

    while ($result_preference2 = mysqli_fetch_assoc($result_preferencea)) {

        $groupedData[$result_preference2['sub_preference_id']][] = $result_preference2;
    }

}
但是我失败了?

任何想法如何通过使用选择查询,而不是使用PHP foreach,数组等选择数据?

2 个答案:

答案 0 :(得分:1)

使用PDO:

$in = join(',', array_fill(0, count($preference_ids), '?'));
$select = <<<SQL
    SELECT * 
    FROM gic_user_preference 
    WHERE preference_id IN ($in)
    GROUP BY preference_id, Sub_Preference_Id;
SQL;
$statement = $pdo->prepare($select);
$statement->execute($preference_ids);

使用MySQLi

$in = join(',', array_fill(0, count($preference_ids), '?'));
$select = <<<SQL
    SELECT * 
    FROM gic_user_preference 
    WHERE preference_id IN ($in)
    GROUP BY preference_id, Sub_Preference_Id;
SQL;
$statement = $mysqli->prepare($select);
$statement->bind_param(str_repeat('i', count($preference_ids)), ...$preference_ids);
$statement->execute();
$result = $statement->get_result();

如果您想在单个字段中搜索多个值,请使用&#39;&#39;不等于。你还需要防止sql注入。

答案 1 :(得分:1)

你不需要任何东西。只需循环遍历每条记录,并根据偏好ID和subPreferenceID创建一个数组:

例如:

    $id = 29;
$post = get_post($id);

if($post) {
    $title = get_the_title($post->ID);
    $content = do_shortcode( $post->post_content );
    $meta = get_post_meta($id, 'example_meta', true); // to make sure meta_key already exists for this posts.
    echo "<h4 class='col-12'>", $title, "</h4>";
    echo "<h2 class='col-12 col-md-11 col-lg-8 py-5'>", get_the_subtitle($post), "</h2>";
    echo $content;
    echo $meta;
}