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
0 answers

Pass value in a REGEXP expression MySQL, MariaDB

Having this field in my database "description": I want to update…
0
votes
0 answers

python mysql syntax error when declaring and printing a variable

For some reason, this simple block of code is giving me a sql syntax error. I have no clue why it's wrong result = conn.execute( text( ''' SET @var = 1; SELECT @var; ''' ) ) print(result.fetchall()) Why is…
0
votes
1 answer

Mysql select query with ordering issue

I want to display one paid status(1) column and then 3 unpaid(0) status column respectively. Paid status is taken based on Expirydate, the date greater than now is considered paid status and date which is less than expiry date is considered unpaid…
0
votes
1 answer

Use the result of a MySQL query, as a variable, with the IN-operator

Looking for a way to return a variable that can be used inside a IN-operator. My current result returns: 1,2,3, but I guess the variable should be more like this: '1','2','3' to be able to use it. Is this possible? Or should I try something else…
Aldo
  • 730
  • 8
  • 20
0
votes
1 answer

MYSQL Strict Mode

For STRICT_ALL_TABLES, MySQL returns an error and ignores the rest of the rows. However, because the earlier rows have been inserted or updated, the result is a partial update. To avoid this, use single-row statements, which can be aborted without…
0
votes
1 answer

In MySQL, how can you replace variables by using WITH?

I have a query for comparing statement balances with expense records. I would like to make it a View, but it is using two variables, which you can't use in a View. It occurs to me that it should be possible to use a Common Table Expressions (CTEs,…
0
votes
0 answers

How to use MySQL variables in PHP?

I am trying to do something like this: SET @grade_id = (SELECT grade_id FROM grades_table WHERE grades = '1' AND sections = 'A'); SET @subject_id = (SELECT subject_id FROM subjects_table WHERE grade_id = @grade_id AND subject_name = 'Maths' AND…
0
votes
3 answers

Deleting records in MySQL WHERE id IN (@VARIABLE) -- (2,3,4)

Is there is a way to delete records using WHERE IN @VARIABLE? -- DEMO TABLE CREATE TABLE people ( id int AUTO_INCREMENT NOT NULL, name varchar(100), age int, active smallint DEFAULT 0, PRIMARY KEY (id) ); -- DEMO DATA INSERT…
DevWL
  • 17,345
  • 6
  • 90
  • 86
0
votes
0 answers

Using a mysql variable as parameter for AGAINST

I am fetching data from an excelsheet using LOAD DATA INFILE statement and trying to evaluate the results before saving to a database. I need to be able to pass one of the fields from the excel as a mysql variable in the AGAINST() function like…
Peter
  • 60
  • 8
0
votes
1 answer

Using the input parameters from stored parameters as variables inside where clause MySQL

I have a simple mysql table: CREATE TABLE `cont` ( `ID` int(11) NOT NULL, `Meeting_id` int(11) DEFAULT NULL, `member_name` varchar(20) NOT NULL, `cont_prod` varchar(20) NOT NULL, `start_date` date NOT NULL, `type_of` varchar(100), …
0
votes
1 answer

How to store entire column values of the query result in MYSQL?

I have a statement, select customer_id,count(customer_id), sum(total_price), date_time from the_order group by customer_id; The Result, cus1 15 437700 2021-04-23 cus2 1 12000 2008-5-9 cus3 1 13000 2009-5-9 cus4 1 14000 …
Aman Chaudhary
  • 802
  • 3
  • 10
  • 27
0
votes
1 answer

Run different SQL statements using If Then with a variable

Trying to do something very simple. Using If/Then, is there a way to run a separate Select statement based on the value of a variable? The function GetTotalActiveUnits() in the below code returns an integer. set @RetVal = …
John
  • 17
  • 6
0
votes
1 answer

How can this MySQL query return NULL?

SET @last_nid=-1; SELECT vid, IF(@last_nid = nid, @last_status, 0) AS last_status, @last_status := status AS status, @last_nid := nid AS nid FROM node_field_revision ORDER BY vid; nid and vid are both positive integers and status…
chx
  • 11,270
  • 7
  • 55
  • 129
0
votes
1 answer

Declare a variable based on name in sys.tables then delete the table based on that variable in dynamic SQL

So what I expect is for the first piece of code to find the table name then if that table name exists and is more than 3 days old drop that table. My issue with this code is that the code is not replacing @temp_name with the actual table DrinkSales.…
Jessica Warren
  • 378
  • 1
  • 4
  • 15
0
votes
1 answer

Must declare scalar variable in a unit test

I am trying to design a unit test that ensures that a message is bigger than a certain number of characters. My code is: EXEC tSQLt.NewTestClass 'TestMarketingEnoughRows'; GO CREATE OR ALTER PROCEDURE TestMarketing.[test that…