mysql-variables

SQL query performance improvement for advice

巧了我就是萌 提交于 2019-12-06 08:47:52
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 | | 5 | 4.00 | | 6 | 3.65 | +----+-------+ For example, given the above Scores table, your query should

Executing a stored procedure with cursor in PHP

左心房为你撑大大i 提交于 2019-12-04 05:59:34
问题 I have a stored procedure that I am trying to call from my php. Here is the stored procedure: BEGIN DECLARE done INT DEFAULT FALSE; declare phone_temp VARCHAR(20) default ''; declare phone_cur cursor for SELECT DISTINCT sentNum FROM Queue; declare continue handler for not found set done = true; #create temp table create temporary table if not exists temp_return AS SELECT * FROM Queue LIMIT 0; #empty if exists delete from temp_return; open phone_cur; phone_loop: LOOP fetch phone_cur into phone

MySQL: Count of records with consecutive months

早过忘川 提交于 2019-12-04 01:21:58
问题 I've searched around for this, but all the similar questions and answers are just different enough not to work. I have a table with the following fields: person, thing, purdate. A new record is entered when a person buys each new thing. I want to count the consecutive months that a person bought any "thing" (thing01 or thing02, it doesn't mater). If there is a break in consecutive purdays, then the count should start over. With the data enclosed, I want to end up with this: | Person | Consec

Executing a stored procedure with cursor in PHP

大兔子大兔子 提交于 2019-12-02 08:48:06
I have a stored procedure that I am trying to call from my php. Here is the stored procedure: BEGIN DECLARE done INT DEFAULT FALSE; declare phone_temp VARCHAR(20) default ''; declare phone_cur cursor for SELECT DISTINCT sentNum FROM Queue; declare continue handler for not found set done = true; #create temp table create temporary table if not exists temp_return AS SELECT * FROM Queue LIMIT 0; #empty if exists delete from temp_return; open phone_cur; phone_loop: LOOP fetch phone_cur into phone_temp; if done then leave phone_loop; end if; insert into temp_return SELECT * FROM Queue WHERE num2

MySQL: Count of records with consecutive months

有些话、适合烂在心里 提交于 2019-12-01 05:22:33
I've searched around for this, but all the similar questions and answers are just different enough not to work. I have a table with the following fields: person, thing, purdate. A new record is entered when a person buys each new thing. I want to count the consecutive months that a person bought any "thing" (thing01 or thing02, it doesn't mater). If there is a break in consecutive purdays, then the count should start over. With the data enclosed, I want to end up with this: | Person | Consec Days | | person_01 | 3 | | person_02 | 3 | | person_02 | 2 | I know I can get a distinct list of person

Combining condition from two columns mysql

落爺英雄遲暮 提交于 2019-11-28 12:42:10
I would like to combine conditions from 2 different columns for my query. This is my original query. You can test it in sqlfiddle.com. -- creating database first for test data create table attendance(Id int, DateTime datetime, Door char(20)); INSERT INTO attendance VALUES ( 1, '2016-01-01 08:00:00', 'In'), ( 2, '2016-01-01 09:00:00', 'Out'), ( 3, '2016-01-01 09:15:00', 'In'), ( 4, '2016-01-01 09:30:00', 'In'), ( 5, '2016-01-01 10:00:00', 'Out'), ( 6, '2016-01-01 15:00:00', 'In'); SELECT * FROM attendance; SELECT @id:=@id+1 Id, MAX(IF(Door = 'In', DateTime, NULL)) `Check In`, MAX(IF(Door = 'Out

Combining condition from two columns mysql

允我心安 提交于 2019-11-27 07:12:38
问题 I would like to combine conditions from 2 different columns for my query. This is my original query. You can test it in sqlfiddle.com. -- creating database first for test data create table attendance(Id int, DateTime datetime, Door char(20)); INSERT INTO attendance VALUES ( 1, '2016-01-01 08:00:00', 'In'), ( 2, '2016-01-01 09:00:00', 'Out'), ( 3, '2016-01-01 09:15:00', 'In'), ( 4, '2016-01-01 09:30:00', 'In'), ( 5, '2016-01-01 10:00:00', 'Out'), ( 6, '2016-01-01 15:00:00', 'In'); SELECT *

Rank users in mysql by their points

守給你的承諾、 提交于 2019-11-27 06:24:14
问题 I am trying to rank my students by their points that I've calculated before but the problem is if students have same points they both should be in same rank E.g Student 1 has full points Student 2 has full points they both have to be rank as 1; Here an example of my database the query I am trying to do is (just for select then I can insert the values to my column) SELECT a.points count(b.points)+1 as rank FROM examresults a left join examresults b on a.points>b.points group by a.points; Edit

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

主宰稳场 提交于 2019-11-26 22:29:54
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? 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 guaranteed. The order of evaluation for expressions involving user variables is undefined and may change based on

Get top n records for each group of grouped results

这一生的挚爱 提交于 2019-11-25 21:36:45
问题 The following is the simplest possible example, though any solution should be able to scale to however many n top results are needed: Given a table like that below, with person, group, and age columns, how would you get the 2 oldest people in each group? (Ties within groups should not yield more results, but give the first 2 in alphabetical order) +--------+-------+-----+ | Person | Group | Age | +--------+-------+-----+ | Bob | 1 | 32 | | Jill | 1 | 34 | | Shawn | 1 | 42 | | Jake | 2 | 29 |