Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding one extra row to the result of MySQL select query

Tags:

mysql

I have a MySQL table like this

id   Name   count
1    ABC    1
2    CDF    3
3    FGH    4

using simply select query I get the values as

1    ABC    1
2    CDF    3
3    FGH    4

How I can get the result like this

1    ABC    1
2    CDF    3
3    FGH    4
4    NULL   0

You can see Last row. When Records are finished an extra row in this format last_id+1, Null ,0 should be added. You can see above. Even I have no such row in my original table. There may be N rows not fixed 3,4

like image 216
Warrior Avatar asked Aug 24 '12 13:08

Warrior


People also ask

How do I add a row to a select query?

To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.

How do I add a second row in MySQL?

You need to use ORDER BY clause to get the second last row of a table in MySQL. The syntax is as follows. select *from yourTableName order by yourColumnName DESC LIMIT 1,1; To understand the above syntax, let us create a table.

How do you append a row in SQL?

Here is the basic syntax for adding rows to a table in SQL: INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc); The first line of code uses the INSERT statement followed by the name of the table you want to add the data to.

How can I add multiple values in one column in MySQL?

First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name. Third, specify a comma-separated list of row data in the VALUES clause. Each element of the list represents a row.


1 Answers

The answer is very simple

select (select max(id) from mytable)+1 as id, NULL as Name, 0 as count union all select id,Name,count from mytable;
like image 107
Abhishek Kulkarni Avatar answered Sep 27 '22 01:09

Abhishek Kulkarni