Found 4378 Articles for MySQL

Get all the tables from a MySQL database having a specific column, let’s say xyz?

AmitDiwan
Updated on 27-Sep-2019 08:00:45

158 Views

Let’s say we have a database “web” and we need to get all the tables having a specific column ’StudentFirstName’.For this, below is the query −mysql> select myColumnName.table_name from information_schema.columns myColumnName where myColumnName.column_name = 'StudentFirstName' and table_schema='web';This will produce the following output −+---------------+ | TABLE_NAME | +---------------+ | demotable109 | | demotable297 | | demotable335 | | demotable395 | | demotable418 | | demotable425 | | demotable436 | +---------------+ 7 rows in set (0.14 sec)Therefore, the above tables have one of the column names as “StudentFirstName”.Let us check the description ... Read More

Convert MySQL timestamp to UNIX Timestamp?

AmitDiwan
Updated on 27-Sep-2019 07:58:58

1K+ Views

To convert MySQL timestamp to UNIX Timestamp, use the UNIX_TIMESTAMP(). Following is the syntax −select unix_timestamp(yourColumnName) from yourTableName;Let us first create a table −mysql> create table DemoTable(    Duetimestamp timestamp ); Query OK, 0 rows affected (2.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(now()); Query OK, 1 row affected (1.53 sec) mysql> insert into DemoTable values('2016-01-21 12:34:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-05-01 02:00:00'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable values('2017-03-02 01:10:20'); Query OK, 1 row affected (10.19 sec)Display all records from ... Read More

How can I replace & with an ampersand in my MySQL database?

AmitDiwan
Updated on 27-Sep-2019 07:57:41

594 Views

To replace & with an ampersand, use MySQL REPLACE(). Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value varchar(100) ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values('@amp'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(Value) values('&'); Query OK, 1 row affected (1.09 sec) mysql> insert into DemoTable(Value) values('#amp'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----+-------+ ... Read More

Find average on the basis of corresponding duplicate VARCHAR values in MySQL

AmitDiwan
Updated on 27-Sep-2019 07:56:21

65 Views

Let us first create a table −mysql> create table DemoTable(    Value int,    Value2 varchar(100) ); Query OK, 0 rows affected (0.84 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, '999.999.999.999'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20, '888.888.888.888'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(30, '999.999.999.999'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-------+-----------------+ | Value | Value2 ... Read More

Can we perform MySQL UPDATE and change nothing in a table?

AmitDiwan
Updated on 27-Sep-2019 07:55:11

297 Views

Yes, we can do that. Let us first create a table −mysql> create table DemoTable(    Id int ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(201); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(202); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(290); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(301); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------+ ... Read More

What does select @@identity do in MySQL?

AmitDiwan
Updated on 27-Sep-2019 07:53:46

1K+ Views

The @@identity returns the last inserted value in the auto_increment column in the current session. Let us first create a table −mysql> create table DemoTable(    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(100) ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(UserName) values('David'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(UserName) values('Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(UserName) values('Carol'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(UserName) values('Robert'); Query OK, 1 row affected (0.24 sec) ... Read More

MySQL query to fetch the maximum corresponding value from duplicate column values

AmitDiwan
Updated on 27-Sep-2019 07:52:33

138 Views

Let us first create a table −mysql> create table DemoTable(    ProductName varchar(100),    ProductPrice int ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Product-1', 56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Product-2', 78); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('Product-1', 88); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Product-2', 86); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Product-1', 45); Query OK, 1 row affected (0.18 sec) mysql> insert into ... Read More

Will extending the size of a varchar field in MySQL affect the data inside it?

AmitDiwan
Updated on 27-Sep-2019 07:50:35

367 Views

If you will extend the size of a varchar field in MySQL then it won’t affect the data inside it.Let us first create a table −mysql> create table DemoTable(    Name varchar(8) ); Query OK, 0 rows affected (1.11 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.13 sec)Display all records from the ... Read More

Sort a list in MySQL and display a fixed result at the end of the column?

AmitDiwan
Updated on 27-Sep-2019 07:48:52

55 Views

Let us first create a table −mysql> create table DemoTable(    FirstName varchar(100) ); Query OK, 0 rows affected (0.97 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values('Adam'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.27 ... Read More

Fetch random rows from a table with MySQL

AmitDiwan
Updated on 27-Sep-2019 07:47:30

105 Views

For this, you can use a PREPARE statement. Let us first create a table −mysql> create table DemoTable(    FirstName varchar(100),    CountryName varchar(100) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Adam', 'US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Chris', 'AUS'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert', 'UK'); Query OK, 1 row affected (0.32 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+-----------+-------------+ | FirstName | ... Read More

Advertisements