MySQL query to find the average of rows with the same ID

AmitDiwan
Updated on 27-Sep-2019 06:58:20

420 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int,    StudentMarks int ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1000, 78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(1001, 88); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(1000, 89); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(1000, 67); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1000, 90); Query OK, 1 row affected (0.21 sec) mysql> insert ... Read More

How can I create a MySQL boolean column and assign value 1 while altering the same column?

AmitDiwan
Updated on 27-Sep-2019 06:55:48

126 Views

To assign value 1 while altering, use the MySQL DEFAULT. This will itself enter 1 if nothing is inserted in the same column while using the INSERT command.Let us first create a table −mysql> create table DemoTable (    isAdult int ); Query OK, 0 rows affected (1.39 sec)Following is how you can assign value 1 default to the already created column −mysql> alter table DemoTable CHANGE isAdult isAdult BOOLEAN DEFAULT '1' NOT NULL; Query OK, 0 rows affected (1.18 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable values(); ... Read More

How to find the minimum and maximum values in a single MySQL Query?

AmitDiwan
Updated on 27-Sep-2019 06:53:29

294 Views

To find the minimum and maximum values in a single query, use MySQL UNION. Let us first create a table −mysql> create table DemoTable (    Price int ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(120); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement ... Read More

MySQL query to find the number of rows in the last query

AmitDiwan
Updated on 27-Sep-2019 06:51:16

136 Views

For this, use the FOUND_ROWS in MySQL. Following is the syntax −SELECT SQL_CALC_FOUND_ROWS TABLE_NAME FROM `information_schema`.tables WHERE TABLE_NAME LIKE "yourValue%" LIMIT yourLimitValue;Here, I am using the database ‘web’ and I have lots of tables, let’s say which begins from DemoTable29. Let us implement the above syntax to fetch only 4 of such rows −mysql> SELECT SQL_CALC_FOUND_ROWS TABLE_NAME FROM `information_schema`.tables WHERE TABLE_NAME LIKE "DemoTable29%" LIMIT 4;This will produce the following output −+--------------+ | TABLE_NAME | +--------------+ | demotable29 | | demotable290 | | demotable291 | | demotable292 | +--------------+ 4 rows in set (0.01 sec)Here is the query ... Read More

Get only the date from datetime in MySQL?

AmitDiwan
Updated on 27-Sep-2019 06:49:22

1K+ Views

To get only the date from DateTime, use the date format specifiers −%d for day %m for month %Y for yearLet us first create a table −mysql> create table DemoTable (    AdmissionDate datetime ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-07-21 12:34:56'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2016-08-18 10:00:02'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values('2018-01-03 11:02:20'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from ... Read More

Replace only a specific value from a column in MySQL

AmitDiwan
Updated on 27-Sep-2019 06:47:28

7K+ Views

To replace, use the REPLACE() MySQL function. Since you need to update the table for this, use the UPDATE() function with the SET clause.Following is the syntax −update yourTableName set yourColumnName=replace(yourColumnName, yourOldValue, yourNewValue);Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    CountryName varchar(100) ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 'AUS'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Bob', 'AUS'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Chris', 'US'); Query OK, 1 ... Read More

MySQL query to place a specific record on the top

AmitDiwan
Updated on 27-Sep-2019 06:44:35

115 Views

For this, you can use the ORDER BY CASE statement. Let us first create a table −mysql> create table DemoTable (    StudentName varchar(100),    StudentMarks int ); Query OK, 0 rows affected (0.97 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 45); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('John', 67); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('David', 89); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('John', 98); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Mike', ... Read More

MySQL query to order by the first number in a set of numbers?

AmitDiwan
Updated on 27-Sep-2019 06:41:39

132 Views

To order by the first number in a set of numbers, use ORDER BY SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable (    SetOfNumbers text ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('245, 654, 76, 89, 98'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2000, 567, 9090, 6789'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('1001, 90595, 657, 99'); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> ... Read More

Can we use reserved word ‘index’ as MySQL column name?

AmitDiwan
Updated on 27-Sep-2019 06:39:27

485 Views

Yes, but you need to add a backtick symbol to the reserved word (index) to avoid error while using it as a column name.Let us first create a table −mysql> create table DemoTable (    `index` int ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1000); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(1020); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(967); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(567); Query OK, 1 row affected (0.12 ... Read More

Getting maximum from a column value and set it for all other values in the same column with MySQL?

AmitDiwan
Updated on 27-Sep-2019 06:31:40

50 Views

Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    Score int ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('David', 59); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris', 97); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Bob', 98); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Carol', 91); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

Advertisements