简体   繁体   English

从两个mysql表中选择一个有限制日期的随机记录?

[英]Select a random record from two mysql tables with limited date?

I have two tables 我有两张桌子

CREATE TABLE `shares` (
  `id` int(11) NOT NULL auto_increment,
  `entry_id` int(11) default NULL,
  `service` int(11) default NULL,
  `created_date` datetime default NULL,
  `ip` varchar(45) default NULL,
  PRIMARY KEY  (`id`),
  KEY `entry_id` (`entry_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2570 DEFAULT CHARSET=latin1;

CREATE TABLE `entries` (
  `id` int(11) NOT NULL auto_increment,
  `first_name` varchar(255) default NULL,
  `last_name` varchar(255) default NULL,
  `street_address` varchar(255) default NULL,
  `city` varchar(255) default NULL,
  `postal_code` varchar(255) default NULL,
  `province` varchar(255) default NULL,
  `daytime_phone` varchar(255) default NULL,
  `email_address` varchar(255) default NULL,
  `tos` int(11) default NULL,
  `subscribe` int(11) default NULL,
  `ip` varchar(45) default NULL,
  `created_date` datetime default NULL,
  `pin` varchar(255) default NULL,
  `return` int(11) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  KEY `email_address` (`email_address`)
) ENGINE=InnoDB AUTO_INCREMENT=36503 DEFAULT CHARSET=latin1;

And I need to generate 10 random records from both tables. 我需要从两个表中生成10条随机记录。

Info: Each share is a single extra ballot, but people can share using multiple services (each service is still a single extra ballot) and share multiple times (which is still a single extra ballot). 信息:每次共享都是一次额外的投票,但是人们可以使用多种服务进行共享(每个服务仍然是一次额外的投票),并且可以共享多次(仍然是一次额外的投票)。

So I need to random 10 records from entries, taking into account if a single entry id exists then select from that also. 因此,我需要从条目中随机抽取10条记录,并考虑是否存在单个条目ID,然后再从中选择。

Example, 例,

User 1
User 2
User 3
User 4 1 Share
User 5
User 6 1 Share
User 7 2 Shares
User 8
User 9
User 10 3 Shares

It would take that and generate a random record where 它会生成一个随机记录,其中

User 1 (1 chance)
User 2 (1 chance)
User 3 (1 chance)
User 4 1 Share (2 chances)
User 5 (1 chance)
User 6 1 Share (2 chance)
User 7 2 Shares (2 chance)
User 8 (1 chance)
User 9 (1 chance)
User 10 3 Shares (2 chance)

I don't know how I can do this? 我不知道该怎么做? Any help 任何帮助

My approach would be to create an array of 10 random ids from 'entries', then see if those ids exist in 'shares'. 我的方法是根据“条目”创建10个随机ID的数组,然后查看这些ID是否存在于“共享”中。 If they do, put that id into the array a second time, giving them that second chance. 如果是这样,请再次将该ID放入数组,从而给他们第二次机会。 Then it is a random selection from that end array. 然后是从该末端数组中随机选择。

One way to do this could be something like: 一种实现方式可能是:

$entryIdArray = array();
$query = mysql_query(SELECT * FROM entries ORDER BY RAND LIMIT 10);
while($row = mysql_fetch_array($query)){
   $entryIdArray[] = $row['id'];
}


foreach($entryIdArray as $thisEntrant){
   $query = mysql_query(SELECT * FROM shares WHERE entry_id = $thisEntrant);
   if(mysql_num_rows($query) > 0){
      $entryIdArray[] = $thisEntrant;
   }
}

$winningEntrantId = $entryIdArray[rand(0, sizeof($entryIdArray)-1)];

I've never tried adding to an array while looping through that same array, so you may have to make a separate array to add all the ids to that one instead, but this shows the basic idea. 在循环遍历同一数组时,我从未尝试添加到数组,因此您可能不得不制作一个单独的数组以将所有id添加到该数组,但这显示了基本思想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM