mysql中,怎么每隔10条抽一条记录,多谢
mysql中,如何每隔10条抽一条记录,谢谢!
<br />$sql="SELECT `cid` FROM `ks` where `cid`='' ORDER BY rand() LIMIT 100";<br />$r = $db->getAll($sql); <br />foreach($r as $v){<br />$sql="select `cid` from `member` WHERE `cid` not in (select `cid` from `ks`) ORDER BY rand() LIMIT 1";<br />$row = $db->getRow($sql);<br />$cid=$v['cid'];<br />$sql="update `ks` set `userid`='".$row['id']."' WHERE `cid`='$cid'"; <br />}<br />
这样能在member表里随机抽一条随机写入ks表
怎么才能做到member表里面如果CID相同就错开10条随机写入ks表?
或者代码应该怎么写才能达到这样的目的:
member表里有几千条数据,如果member表里CID相同的数据就错开10条写入ks表,不相同的就随机写入ks表
可能我表达得不是很清楚,这是一个考试随机安排座位的程序,cid是城市ID,如果城市ID相同就错开位置安排座位,防止作弊。
先谢谢了,因为分数没有多少了,给完也才20分,见凉!
------解决思路----------------------
$sql1 = "select * from `member` WHERE status = 0 LIMIT 1";<br />$r1 = $db->getRow($sql1); <br />$cur_cid = $r1['cid'];//查看当前第一个考生的所在城市<br />$sql4 = "insert into `ks` (`cid`,`userid`) values ({$r1['cid']},{$r1['cid']})";<br />$db->query($sql4);//先插入第一个考生信息<br /><br />push_next($cur_cid);//接二连三的递推插入<br /><br />function push_next($cur_cid){<br /> $sql2 = "select * from `member` WHERE status = 0 and cid != {$cur_cid} LIMIT 1";<br /> $r2 = $db->getRow($sql2);<br /> $sql3 = "insert into `ks` (`cid`,`userid`) values ({$r2['cid']},{$r2['cid']})";<br /> $db->query($sql3);<br /> push_next($sql2['cid']);<br />}
可以试试
------解决思路----------------------
首先,先要确定以下几个问题:
1.考场可以容纳多少考生?(即一次需要往KS表中写入的记录条数)
2.学员表中的学员来自几个城市(select distinct cid from members)
确定上上面两个问题,就可以确定程序会在每个城市里选取几位学员安排在同一考场。比如,考场可容纳30位考生,学员来自5个不同的城市,那么程序会在每个城市选取6位学员安排在同一考场。
创建一个二维数组,第一个维度是城市ID,第二个维度就是学员ID,做一个双重循环把学员写入KS表中即可。
具体代码我就不写了,下面把这个数组结构描述一下:
<br />candidates = array (array("cid" => 1, "members" => array(1, 11, 21, 31)), <br /> array("cid" => 2, "members" => array(2, 12, 22, 32)),<br /> array("cid" => 3, "members" => array(3, 13, 23, 33)));<br />
这个只考虑到了左右相邻的考学来自不同的城市,你可以扩展一下,将前后相邻的判断加到代码逻辑中。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.
