Found 4378 Articles for MySQL

How to TRIM x number of characters, beginning from the last in MySQL?

AmitDiwan
Updated on 05-Nov-2019 06:45:55

202 Views

For this, you can use substring() along with length(). Let us first create a table −mysql> create table DemoTable1329    -> (    -> StudentName varchar(40)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1329 values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1329 values('Chris Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1329 values('Adam Smith'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1329 values('John Doe'); Query OK, 1 row affected (0.44 sec)Display all records from the ... Read More

Insert values in a table by MySQL SELECT from another table in MySQL?

AmitDiwan
Updated on 05-Nov-2019 06:41:53

790 Views

Fir this, use INSERT INTO SELECT statement. Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int,    -> Name varchar(20),    -> Age int    -> ); Query OK, 0 rows affected (1.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(100, 'Chris', 24); Query OK, 1 row affected (0.61 sec) mysql> insert into DemoTable1 values(101, 'Adam', 23); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1 values(102, 'John', 25); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(103, 'Carol', 26); Query ... Read More

MySQL query to extract time in a format without seconds

AmitDiwan
Updated on 05-Nov-2019 06:38:25

302 Views

For this, you can use time_format(). Let us first create a table −mysql> create table DemoTable1326    -> (    -> Arrivaltime time    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1326 values('12:10:45'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1326 values('20:00:00'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1326 values('22:45:55'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1326 values('04:10:24'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select * ... Read More

Order MySQL results without identifier?

AmitDiwan
Updated on 05-Nov-2019 06:35:51

93 Views

To order MySQL results without identifier, the syntax is as follows −select * from yourTableName order by 1 DESC LIMIT yourLimitValue;Let us first create a table −mysql> create table DemoTable1325    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1325 values(100, 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1325 values(101, 'Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1325 values(120, 'David'); Query OK, 1 row affected (0.14 sec) mysql> insert ... Read More

How to identify composite primary key in any MySQL database table?

AmitDiwan
Updated on 05-Nov-2019 06:28:38

1K+ Views

You can use aggregate function count(*). If it returns a value greater than 1, that would mean the table has composite primary key.Let us first create a table −mysql> create table DemoTable1324    -> (    -> StudentId int,    -> StudentName varchar(20),    -> StudentAge int,    -> StudentCountryName varchar(20)    -> ); Query OK, 0 rows affected (0.52 sec)Here is the query to add composite primary key −mysql> alter table DemoTable1324 ADD CONSTRAINT constr_IdAgeCountry PRIMARY KEY (StudentId, StudentAge, StudentCountryName); Query OK, 0 rows affected (1.29 sec) Records: 0 Duplicates: 0 Warnings: 0Following is the query to identify composite ... Read More

MySQL query to get the last created table name (most recent)?

AmitDiwan
Updated on 05-Nov-2019 06:26:19

190 Views

You can use the concept INFORMATION_SCHEMA.TABLES for this. Let us first create a table. This would be our most recent table −mysql> create table DemoTable1323    -> (    -> FirstName varchar(10)    -> ); Query OK, 0 rows affected (0.43 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1323 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1323 values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1323 values('Bob'); Query OK, 1 row affected (0.11 sec)Display all records from the table using select statement −mysql> select *from DemoTable1323;This will produce ... Read More

How to search dob in a table, which is in the yyyy-mm-dd structure and compare only with a specific yyyy format (year) in MySQL?

AmitDiwan
Updated on 08-Jul-2020 08:08:15

82 Views

For this, use MySQL YEAR() as in the below syntax −select * from yourTableName where year(yourColumnName)=’yourYearValue’;Let us first create a table −mysql> create table DemoTable1322 -> ( -> DOB date -> ); Query OK, 0 rows affected (0.55 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable1322 values('1999-04-12'); Query OK, 1 row affected (0.68 sec) mysql> insert into DemoTable1322 values('2010-12-01'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1322 values('2015-03-09'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1322 values('2007-05-24'); Query OK, 1 row affected (0.08 sec)Display all records from the table ... Read More

MySQL query to count the dates and fetch repeated dates as well

AmitDiwan
Updated on 08-Jul-2020 08:08:40

186 Views

To display the count, use aggregate function COUNT(*). Let us first create a table −mysql> create table DemoTable1321 -> ( -> ArrivalDatetime timestamp -> ); Query OK, 0 rows affected (0.50 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable1321 values(now()); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1321 values('2019-01-10 12:34:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1321 values('2019-06-12 11:34:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1321 values('2019-06-12 04:50:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1321 values('2019-09-18 10:50:45'); Query OK, 1 ... Read More

Output only the name of the month instead of the month number in MySQL

AmitDiwan
Updated on 08-Jul-2020 08:09:12

88 Views

To display only the month number, use DATE_FORMAT() along with STR_TO_DATE(). Let us first create a table:mysql> create table DemoTable1320 -> ( -> MonthName varchar(20) -> ); Query OK, 0 rows affected (0.43 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable1320 values('10/10/2010'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1320 values('11/12/2018'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1320 values('12/01/2019'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable1320;Output+------------+ | MonthName  | +------------+ | 10/10/2010 | | 11/12/2018 | ... Read More

Create a MySQL table from already created table selecting specific rows?

AmitDiwan
Updated on 04-Nov-2019 11:11:01

99 Views

To create a table from an already created table, use CREATE TABLE AS SELECT statement. Let us first create a table −mysql> create table DemoTable1318 -> ( -> Id int, -> FirstName varchar(10), -> LastName varchar(10), -> Age int -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1318 values(1, 'Chris', 'Brown', 21); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1318 values(2, 'David', 'Miller', 24); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1318 values(3, 'Carol', 'Taylor', 23); Query OK, 1 row affected (0.11 ... Read More

Advertisements