Because with the new table I have to populate a dropdownlist with the two dates extracted from the random query, to be able to filter the gridview by single date
Sorry but that’s too complicated for me. Why do you need a new table with data you received from a query? Use the output of the query and fill your drop-down with this data. No need to write it to another table
Prepare the statement. This means telling MySQL what SQL statement you want to execute, but without specifying the actual values for the parameters. Instead, you use placeholder characters, such as ? .
Bind the values to the placeholders. This means telling MySQL the actual values that you want to use for the parameters in the prepared statement.
Execute the prepared statement. This means running the SQL statement with the actual values bound to the parameters.
Here is an example:
# Prepare the statement.
PREPARE bind_statement FROM 'SELECT * FROM table WHERE date BETWEEN ? AND ?';
# Bind the values to the placeholders.
SET @variable_a = '2023-10-07';
SET @variable_b = '2023-10-18';
# Execute the prepared statement.
EXECUTE bind_statement USING @variable_a, @variable_b;
This will execute the following SQL statement:
SELECT * FROM table WHERE date BETWEEN '2023-10-07' AND '2023-10-18';
The results of the query will be stored in a temporary result set. You can then access the results of the query using the FETCH statement.
Here is an example of how to access the results of the query:
# Fetch the next row from the result set.
FETCH NEXT INTO @variable_a, @variable_b FROM bind_statement;
# Use the values of the variables.
SELECT @variable_a, @variable_b;
This will print the following output:
2023-10-07 2023-10-18
Binding variables is a useful technique for preventing SQL injection attacks and improving the performance of your queries.
Gonna be honest, if that post is too complicated for you, you’re out of your depth and just looking for someone to write the code for you for something you’re not ready to create or maintain.
Maybe you’re coming at this the wrong way.
What exactly are you trying to get out of the query at the end?
(I can’t really envision a scenario where a random sorting is a good thing, but you seem to be indicating one)
If I had been able to do it myself I would not have asked the “guru” on this forum for help…
In any case I solved it by using a server-side language (using C# AspNet and the DataTable Compute method, for get Min / Max Date value from DataTable), because my idea on MySql seemed really complicated to implement.
If not yet solved, to bind data extracted from a query to variables in MySQL, you can use the SET command along with a SELECT statement. Below is an example of how you can set the values of the variables A and B using the data from your _tbl_login table:
-- Declare the variables
DECLARE @A DATE;
DECLARE @B DATE;
-- Extract the first date value and assign to variable A
SELECT @A = `_Date`
FROM `_tbl_login`
WHERE `_Date` = '2023-10-07'
LIMIT 1;
-- Extract the second date value and assign to variable B
SELECT @B = `_Date`
FROM `_tbl_login`
WHERE `_Date` = '2023-10-21'
LIMIT 1;
-- To check the assigned values
SELECT @A AS VariableA, @B AS VariableB;
In the above example:
We declared two date variables @A and @B.
The first SELECT query assigns the date '2023-10-07' from the _tbl_login table to the variable @A.
The second SELECT query assigns the date '2023-10-21' from the _tbl_login table to the variable @B.
Finally, we’re using a SELECT statement to display the values assigned to the variables @A and @B.
You can adjust the WHERE clause of the SELECT statements if you want to extract and bind different dates or other values from the table to the variables.
Well putting aside the unnecessary jab at the people helping you here, noone said you had to “do it yourself”. You did, after all, heed our advice and reexamined whether or not this was the correct approach to the problem, rather than trying to brute force it through query.
I’m glad you found a solution to your problem that works for you.