Questions tagged [mysql-variables]

For use with special handling of queries for such things as ranking, grouping, and segmenting. The use of cross-joins that initialize variables, Producing otherwise difficult or impossible queries without them. One should not use this tag merely if you are using variables, in simply fashion.

Pertaining to special use-cases where MySQL User-Defined Variables play an essential or unique role in solving queries or other related MySQL functionality.

These User-Defined variables start with an @ sign, are not DECLARED, and are session-specific. The variables are destroyed upon the close of the connection.

The variables often play a crucial role in special functionality such as ranking, grouping, and special ordering of results. Some of these use-cases include crafting tricky queries to achieve certain, challenging, resultsets.

108 questions
0
votes
1 answer

Optimized way to get top n records of each group

I need top 6 records of each subcategory from a mysql table having almost 100k records. I tried the following mysql query, but I'm concerned about its performance in my table having large number of records. SELECT * FROM (SELECT …
Mike
  • 117
  • 1
  • 3
  • 10
0
votes
0 answers

Mysql query with full text search and offset when values are equal

I have this table called fruits CREATE TABLE fruits ( type varchar(10) NOT NULL, variety varchar(20) NOT NULL, price decimal(5,2) NOT NULL default 0, PRIMARY KEY (type,variety) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; ALTER TABLE fruits…
bento22
  • 1
  • 3
0
votes
2 answers

I want to make a vairable of table can i make it on Microsoft sql server management studio?

I want to make a variable of table in stored procedure to call the data from 3 diffrent tables like i have the city name in textbox and a dropdown list with the values of tablenames, I already make a stored procedure: [dbo].[sp_Search] @City…
0
votes
1 answer

Need help using local variables to subtract previous row value having several groups

I have 3 tables as follows: +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI |…
Idodo
  • 1,322
  • 11
  • 18
0
votes
2 answers

MySQL applys global variables to opened session

I've found that session variables have priority over global variables. Is it possible to apply global variables which is set by "set global" query to opened session so make local one to same with global one? Or, is there any way to wait all opened…
0
votes
1 answer

How to use MySQL variable assignments to iterate through rows?

I have a table with dates, scores and an id which in this case is not unique. Here is a sample data set and SQLFiddle of same: create TABLE variable_test ( date DATE , id INT , score INT ); insert into variable_test (date,id,…
nation161r
  • 51
  • 1
  • 3
  • 14
0
votes
0 answers

Error declaring varchar variable inside MySQL Function

I am attempting to create a MySQL variable inside a stored function similar to this: CREATE DEFINER = CURRENT_USER FUNCTION t_func(cc char(2), lc char(2), os varchar(1000)) RETURNS varchar(1000) DETERMINISTIC READS SQL DATA BEGIN …
Adam
  • 4,590
  • 10
  • 51
  • 84
0
votes
0 answers

Incremental subtotal in MySQL result

I need to calculate - let's say - item prices based on purchase price and date (FIFO/LIFO). So, let's say I've made theese purchases: 3 x ABC for $100 at 2017-11-10 2 x ABC for $80 at 2017-11-12 5 x XYZ for $120 at 2017-11-11 7 x XYZ for $110 at…
Koval
  • 145
  • 8
0
votes
3 answers

SQL Variables use in simple table

select MITARBEITER.NAME, MITARBEITER.GEHALT from MITARBEITER where MITARBEITER.GEHALT = @minGehalt DECLARE @minGehalt as INT SET @minGehalt = (SELECT MIN(MITARBEITER.GEHALT) FROM MITARBEITER) EXEC…
user7772660
0
votes
1 answer

MySql Declaration and Use of Select Variables

I'm struggling to write a query in the correct way. Although it does work I'm given the error: A new statement was found but no delimiter between it and the previous one (near select) Obviously I can still work with this error but #1. I don't…
billcox33
  • 35
  • 3
0
votes
0 answers

Comparing variables inside IF statement in MYSQL

I have 3 tables: wp_names, wp_payments, wp_renewals. I'm using JOIN in student_id on all of them. SELECT s.student_id AS 'Student ID' ,s.student_first_name AS 'First Name' ,@numberofregistrations := count(DISTINCT p.payment_id) as 'Number of…
noahstime
  • 31
  • 2
0
votes
1 answer

Using variables instead of fieldnames in mysql select query

I´ve searched a lot but I can´t figure out how to do it, if it´s possible... I have this table: CREATE TABLE bilanci ( id int AUTO_INCREMENT NOT NULL, medicoid int NOT NULL, `1` int NOT NULL DEFAULT 0, `2` …
0
votes
3 answers

determine actions over period of time PHP MySQL

I don't have any code or database to display, because I am in the planning stage, and I can't figure out the correct way to do this. I want to determine if a user has performed a specific action each day over the course of a week. If they have I…
Bruce
  • 1,039
  • 1
  • 9
  • 31
0
votes
1 answer

MYSQL Update Based On A Row Index Equaling An ID In Another Table

I have two tables. I'd like to update a row in table1 based on the same row index in table2. The IDs don't match but but the ID in table 2 matches the row index. There will always be more data in table 2, but I don't care if the extra rows are…
big_tommy_7bb
  • 1,257
  • 2
  • 21
  • 37
0
votes
1 answer

MYSQL average of n prior rows

I apologize if this question has been asked before. I tried to do searches but may not have been using the same wording as others so my searches came up with nothing. I have a table of just over 6000 rows and I'm trying to do an average of the…
j_allen_morris
  • 559
  • 2
  • 11
  • 26