mysql-variables

select random value based on probability chance

醉酒当歌 提交于 2021-01-18 06:13:14
问题 How do I select a random row from the database based on the probability chance assigned to each row. Example: Make Chance Value ALFA ROMEO 0.0024 20000 AUDI 0.0338 35000 BMW 0.0376 40000 CHEVROLET 0.0087 15000 CITROEN 0.016 15000 ........ How do I select random make name and its value based on the probability it has to be chosen. Would a combination of rand() and ORDER BY work? If so what is the best way to do this? 回答1: You can do this by using rand() and then using a cumulative sum.

select random value based on probability chance

前提是你 提交于 2021-01-18 06:08:56
问题 How do I select a random row from the database based on the probability chance assigned to each row. Example: Make Chance Value ALFA ROMEO 0.0024 20000 AUDI 0.0338 35000 BMW 0.0376 40000 CHEVROLET 0.0087 15000 CITROEN 0.016 15000 ........ How do I select random make name and its value based on the probability it has to be chosen. Would a combination of rand() and ORDER BY work? If so what is the best way to do this? 回答1: You can do this by using rand() and then using a cumulative sum.

select random value based on probability chance

假如想象 提交于 2021-01-18 06:08:10
问题 How do I select a random row from the database based on the probability chance assigned to each row. Example: Make Chance Value ALFA ROMEO 0.0024 20000 AUDI 0.0338 35000 BMW 0.0376 40000 CHEVROLET 0.0087 15000 CITROEN 0.016 15000 ........ How do I select random make name and its value based on the probability it has to be chosen. Would a combination of rand() and ORDER BY work? If so what is the best way to do this? 回答1: You can do this by using rand() and then using a cumulative sum.

SQL query performance improvement for advice

半世苍凉 提交于 2019-12-22 18:05:07
问题 Post the problem statement and current code I am using, and wondering if any smart ideas to improve query performance? Using MySQL. Thanks. Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks. +----+-------+ | Id | Score | +----+-------+ | 1 | 3.50 | | 2 | 3.65 | | 3 | 4.00 | | 4 | 3.85 | |

Precedence of a mysql session variable value in an sql statement

ε祈祈猫儿з 提交于 2019-12-17 20:27:41
问题 What is the standard behaviour of a session variable when used in an SQL statement. Case 1 : In the following example, session variable is behaving as expected. mysql> set @m1=0, @m2=0, @m3=0; Query OK, 0 rows affected (0.00 sec) mysql> mysql> select -> @m1 := 55 m1, @m2 := 42 m2, @m3 := 66 m3, -> @m1, @m2, @m3, -> @b1 := greatest( @m1, @m2, @m3 ) b1, -> @b2 := ( ( @total := @m1 + @m2 + @m3 ) -> - ( @b1 + least( @m1, @m2, @m3 ) )) b2, -> @total total; +----+----+----+------+------+------+----

MySQL - Define a variable within select and use it within the same select

这一生的挚爱 提交于 2019-12-17 06:09:11
问题 Is there a possibility to do something like this? SELECT @z:=SUM(item), 2*@z FROM TableA; I always get NULL for the second column. The strange thing is, that while doing something like SELECT @z:=someProcedure(item), 2*@z FROM TableA; everything works as expected. Why? 回答1: MySQL documentation is quite clear on this: As a general rule, you should never assign a value to a user variable and read the value within the same statement. You might get the results you expect, but this is not

How to rank a record with same weight in MySQL

孤街醉人 提交于 2019-12-12 04:22:15
问题 Suppose I have a data say: Name | Marks StudentA | 90 StudentB | 85 StudentC | 85 StudentD | 70 now StudentA will get 1st Rank, StudentB and StudentC will get 2nd Rank and Student D will get 4th Rank. I know the basic rank computation if there are no duplicate weights, but how to handle if we encounter 2 similar weights as in this case there are two 85 marks which will share rank 2. 回答1: Using Giorgos's fiddle... SELECT name , marks , FIND_IN_SET(marks, (SELECT GROUP_CONCAT(marks ORDER BY

Update single column of table and value should start from 0 and then should increment by 1

柔情痞子 提交于 2019-12-11 10:49:36
问题 $sqlcount1 ="SET uid = -1 UPDATE `agent_normal` SET `uid` = @a:=@a+1"; $result = mysqli_query($conn,$sqlcount1); how to write this query in core php? This query works in mysql but not in core php. Here i want to update a single row of table and value should start from 0 and then should increment by 1. 回答1: There are a couple of problems. Firstly, if you look at this answer you'll see you have an incorrect variable name and missing semicolon. The query should be : $sqlcount1 ="SET @a = -1;

MySQL complex subquery formulation

给你一囗甜甜゛ 提交于 2019-12-11 04:57:45
问题 I have two tables - books and images . books has columns like id , name , releasedate , purchasecount . images has bookid (which is same as the id in books, basically one book can have multiple images. Although I haven't set any foreign key constraint), bucketid , poster (each record points to an image file in a certain bucket, for a certain bookid ). Table schema: poster is unique in images , hence it is a primary key. Covering index on books: ( name , id , releasedate ) Covering index on

Why the order of evaluation for expressions involving user variables is undefined?

有些话、适合烂在心里 提交于 2019-12-08 13:11:33
问题 From MySQL Manual the output of the following query is not guaranteed to be same always. SET @a := 0; SELECT @a AS first, @a := @a + 1 AS second, @a := @a + 1 AS third, @a := @a + 1 AS fourth, @a := @a + 1 AS fifth, @a := @a + 1 AS sixth; Output: first second third fourth fifth sixth 0 1 2 3 4 5 Quoting from the Manual: However,the order of evaluation for expressions involving user variables is undefined; I want to know the story behind. So my question is : Why the order of evaluation for