How to get data from database but from the exact row? - mysql

I am using mysql. Now I want to write that query in which i will give mysql the row number then mysql will data from that column no only.
means..
if i have twenty rows in database but i want to get only 10th no of rows data? so how to write that kind query?

Create an INT field named "ID" that is set to "Auto Increment." This way each row will have a unique ID that you can use to select it. Details about how to do this are located here.

Add a column with the AUTO_INCREMENT flag on it, so that this columns contains the row number for all your rows. MySQL does not really have support for fetching a row by number, only by column value.

Related

Filter specific records from mysql text query

I have a very long text file which contains mysql data (insert commands with data). I want to extract specific data based on id (specific insert string). here is the example of my data.
INSERT INTO tbl_emp_leave_details VALUES
(1405,0,'2020-09-01','2020-09-01',0.5,2,'00:00:00','.5',2,'Some
Personal work','2020-01-09
11:25:26',18,0,0,NULL,NULL,0,0,NULL,''),(4612,1,'2021-05-01','2021-05-01',1,5,'00:00:00','1',-1,'1st
may was holiday this why this is ignored','2021-07-06
10:45:55',1,1,81,'2021-08-01 18:04:59','Approved (Auto
Query)',1,81,'2021-08-02 18:04:59','Approved (Auto Query)'), and so on
I want to filter based on the second column value. if the second column value is 81.
Thanks to #Akina comment i have resolved it.
Created temp table with mysql file
and form temp table I updated my original table

how to populate column from the content of other columns after a row is inserted in the same table in mysql

I have a table traffic with 7 columns, namely toll_id, date1, shift, car_single, car_return, car_local and car_total.
How could I populate first 5 columns manually, and then store a value in column car_total, which will be the sum of car_single and car_return?
Here is the image of my table:
Just to add a 3rd and 4th ways of achieving the desired outcome:
If you have at least MySQL v5.7.6, you can use a generated column as car_total.
Alternatively, you can choose not to store car_total at all, but calculate this value on the fly while querying the table.
Having a column to store the results of the calculation is good if you regularly have search based on that field because you can use indexes to speed up the searches. Calculating the results on the fly may be better, if you just need to display the result of the calculation, but there is no need to filter on it.
There are two ways to do this:
Add the logic in the application itself, so that it calculates total before inserting the record. (Recommended)
Write an after insert trigger (http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html) which calculates the count when record is inserted.

Get the date when the row was inserted MySQL

Is there any way to get a datetime when the row was created? Is it stored anywhere? I just forgot to add a column which tells when the row was inserted, and lots of rows have been inserted and I don't know when they were inserted... Thanks.
Unfortunately there's no way to achieve that. MySQL doesn't store a timestamp of row creation, that's why you always have to create a column to do that, along with ON UPDATE clause which will update the column every time the row is updated
Nope. You need a column to record that, it's not stored in the system otherwise.
There isn't a way to get timestamp when the row was created unless you add a field that is populated with system time or has a default value.

SQL Update with Variable as Parameter

I am working on a stored procedure in MySQL to update a row in a SQL table. What I have is a table where several of the columns are named incrementally. EX: Page_1, Page_2, Page_3....etc.
The data stored in these locations is updated at different times and I have another column to store the number of times the row has been updated. The count variable gets incremented each time the procedure runs and that allows me to utilize it's value in keeping track of where the next update of the data should take place.
From my research I keep finding solutions utilizing "Dynamic SQL." I do not understand how to utilize this to resolve my issue.
I want to pass a variable in to an update statement as the column name.
The code I currently have is as follows.
SET COUNT = COUNT + 1; -- modify count from count column
SET COLUMNLOCATIONVARIABLE = CONCAT('Page_' , COUNT); -- concatenate the count with the column "Prefix"
UPDATE Table
SET COLUMNLOCATIONVARIABLE = INPUTVARIABLE --Use the concatenated statement as the column name and update it with input data
WHERE mainID = INPUTID;
If anyone could explain to me how to pass this variable in as a column name using Dynamic SQL or another solution utilizing non-dynamic SQL I would appreciate it.
Thanks in advance.
This isn't the way to use a database. Create a new table for your Page_* columns, linked to the main table by an identity, with a page number column. Then you can update and include as many pages as you need, addressed by the main identity and the page number.

Mysql sum as last row

Is it possible to have the SUM of all numaric fields in the last of a set of rows?
As of now, I'm using a very simple query such as:
SELECT
*,
SUM((UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start))/3600)
FROM
times
in SQL you cant have a column that appears in only one row, likewise, you also cant have a row that doenst contain all the columns from the other rows.. So having a row that contains something unique is not possible. You can, however, add the calculated column to all rows in the dataset or do the calculation in the calling code after the data is returned.
I think what you are looking for is GROUP BY WITH ROLLUP you will find details on that in the MySQL manual.