mysqli 查询参数内有 50%,另一半是随机的
mysqli query with 50% within parameter and other half random
所以,我在这里纠结于一个问题:
是否可以在我的 sql 中进行类似这样的查询:
- 至少 20% 的结果来自类别 A
- 至少 15% 的结果来自类别 B
- 至少 10% 的结果来自类别 C
- 至少 10% 的结果来自类别 D
和其他 50% 随机(类别 a、b、c、d、e、f、g 等等)?
我已经尝试搜索了一段时间,但没有找到好的答案,所以我希望这里的任何人都能给出一个好的提示。
提前致谢!!
在任何人询问并拒绝我之前,我提出这个问题是为了了解我的网站访问者。这根本不是一个随机问题。
PS:php标签是因为有时候我会用php函数来解决这类问题,网站是php based
我不确定你到底想做什么,但我认为你可以用不同的方法解决你的问题,但无论如何我考虑了你的问题,我想到的唯一想法如下:
*假设您总共有 5000 行,而您只想 SELECT 其中的 50 行,其中这 50 行根据您的百分比进行分配。
$limit = 50;
$cat_a_per = $limit *0.2; // 20% of the results
$cat_b_per = $limit *0.1; // 10% instead of yours 15% because 15% is incorrect ( try to sum percentages up :) )
$cat_c_per = $limit *0.1; // 10% of the results
$cat_d_per = $limit *0.1; // 10% of the results
$rest_per = $limit*0.5; // the rest 50%
// Now create a 5 mysql queries like the following :
"Select * From my_table where cat='A' limit $cat_a_per" ..
"Select * From my_table where cat='B' limit $cat_b_per" ..
"Select * From my_table where cat='C' limit $cat_c_per" ..
"Select * From my_table where cat='D' limit $cat_d_per" ..
"Select * From my_table limit $rest_per" ..
现在将结果汇总到一个数组中或使用 UNION 就可以了...
所以,我在这里纠结于一个问题:
是否可以在我的 sql 中进行类似这样的查询:
- 至少 20% 的结果来自类别 A
- 至少 15% 的结果来自类别 B
- 至少 10% 的结果来自类别 C
- 至少 10% 的结果来自类别 D
和其他 50% 随机(类别 a、b、c、d、e、f、g 等等)?
我已经尝试搜索了一段时间,但没有找到好的答案,所以我希望这里的任何人都能给出一个好的提示。
提前致谢!!
在任何人询问并拒绝我之前,我提出这个问题是为了了解我的网站访问者。这根本不是一个随机问题。
PS:php标签是因为有时候我会用php函数来解决这类问题,网站是php based
我不确定你到底想做什么,但我认为你可以用不同的方法解决你的问题,但无论如何我考虑了你的问题,我想到的唯一想法如下:
*假设您总共有 5000 行,而您只想 SELECT 其中的 50 行,其中这 50 行根据您的百分比进行分配。
$limit = 50;
$cat_a_per = $limit *0.2; // 20% of the results
$cat_b_per = $limit *0.1; // 10% instead of yours 15% because 15% is incorrect ( try to sum percentages up :) )
$cat_c_per = $limit *0.1; // 10% of the results
$cat_d_per = $limit *0.1; // 10% of the results
$rest_per = $limit*0.5; // the rest 50%
// Now create a 5 mysql queries like the following :
"Select * From my_table where cat='A' limit $cat_a_per" ..
"Select * From my_table where cat='B' limit $cat_b_per" ..
"Select * From my_table where cat='C' limit $cat_c_per" ..
"Select * From my_table where cat='D' limit $cat_d_per" ..
"Select * From my_table limit $rest_per" ..
现在将结果汇总到一个数组中或使用 UNION 就可以了...