Found 6705 Articles for Database

MySQL Select Statement DISTINCT for Multiple Columns?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

622 Views

To understand the MySQL select statement DISTINCT for multiple columns, let us see an example and create a table. The query to create a table is as followsmysql> create table selectDistinctDemo    -> (    -> InstructorId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentId int,    -> TechnicalSubject varchar(100)    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command. The query is as followsmysql> insert into selectDistinctDemo(StudentId, TechnicalSubject) values(121, 'Java'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectDistinctDemo(StudentId, TechnicalSubject) values(121, 'MongoDB'); Query OK, 1 row affected (0.16 ... Read More

How to move data between two tables with columns in different MySQL databases?

George John
Updated on 30-Jul-2019 22:30:25

579 Views

For this, you need to use an INSERT SELECT statement. The syntax is as followsINSERT INTO yourDatabaseName1.yourTableName1(yourColumnName1, yourColumnName2, ....N) SELECT yourColumnName1, yourColumnName2, ....N FROM yourdatabaseName2.yourTableName2;Here, I am using the following two databasessampletestLet us create the first table in the “test” databasemysql> use test; Database changed mysql> create table send    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20) -> ); Query OK, 0 rows affected (1.19 sec)Insert some records in the first table using insert command. The query is as followsmysql> insert into send(Name) values('John'); Query OK, 1 row affected ... Read More

How to select first and last row record using LIMIT in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

2K+ Views

Let us first create a table. The query to create a table is as followsmysql> create table FirstAndLastDataDemo    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(20),    -> EmployeeAge int    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query is as followsmysql> insert into FirstAndLastDataDemo(EmployeeName, EmployeeAge) values('John', 23); Query OK, 1 row affected (0.15 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName, EmployeeAge) values('Bob', 13); Query OK, 1 row affected (0.11 sec) mysql> insert into FirstAndLastDataDemo(EmployeeName, EmployeeAge) values('Larry', 24); Query OK, 1 row affected ... Read More

How to display MySQL Table Name with columns?

Samual Sam
Updated on 09-Mar-2020 06:48:28

921 Views

You can use INFORMATION_SCHEMA.COLUMNS table to display MySQL table name with columns. The syntax is as follows −SELECT DISTINCT TABLE_NAME, Column_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'yourDatabaseName';Here, we have a database with the name ‘sample’ with tables. The query is as follows to display table name along with column name −mysql> SELECT DISTINCT TABLE_NAME, Column_Name -> FROM INFORMATION_SCHEMA.COLUMNS -> WHERE TABLE_SCHEMA = 'sample';Output+--------------------------------+-------------------------------+ | TABLE_NAME                     | COLUMN_NAME                   | +--------------------------------+-------------------------------+ | aggregatefunctiondemo          | UserId         ... Read More

How to use actual row count (COUNT(*)) in WHERE clause without writing the same query as subquery in MySql?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

Achieve this with the help of where clause.The syntax is as followsSELECT yourColumnName1, yourColumnName2, ...N FROM yourTableName WHERE (    SELECT COUNT(*) FROM yourTableName )=2;To understand the concept, let us create a table. The query to create a table is as followsmysql> create table CountWithSubqueryDemo    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > EmployeeName varchar(20)    - > ); Query OK, 0 rows affected (2.09 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into CountWithSubqueryDemo(EmployeeName) values('John'); Query OK, 1 row affected (0.54 sec) mysql> ... Read More

Select text after last slash in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

1K+ Views

You need to use substring_index() function from MySQL to select text.The syntax is as followsSELECT substring_index(yourColumnName, '/', -1) AS anyAliasName FROM yourTableName;To understand the above concept, let us create a table. The query to create a table is as followsmysql> create table selectTextAfterLastSlashDemo - > ( - > UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > UserPathDirectory varchar(200) - > ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('C:/MyFolder1/MyEntityFramework'); Query OK, 1 ... Read More

Best way to change the date format in MySQL SELECT?

George John
Updated on 30-Jul-2019 22:30:25

1K+ Views

The best way to change the date format in MySQL SELECT is as followsSELECT DATE_FORMAT(yourColumnName, "%d/%m/%Y %H:%i") AS anyAliasName FROM yourTableName WHERE yourCondition;To understand the above concept, let us create a table. The query to create a table is as followsmysql> create table bestDateFormatDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > ArrivalDateTime datetime - > ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into bestDateFormatDemo(ArrivalDateTime) values(now()); Query OK, ... Read More

Does MySQL have an expanded output flag similar PostgreSQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

143 Views

Yes, you can get expanded out in MySQL using the /G, instead of semicolon(;). The syntax is as followsSELECT *FROM yourTableName\GLet us first create a table as an examplemysql> create table expandedOutputDemo    - > (    - > EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > EmployeeName varchar(20),    - > EmployeeAge int    - > ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into expandedOutputDemo(EmployeeName, EmployeeAge) values('Larry', 27); Query OK, 1 row affected (0.16 sec) mysql> insert into expandedOutputDemo(EmployeeName, EmployeeAge) values('Mike', 29); Query ... Read More

How can I the date of creation and updation of tables in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

252 Views

Get the create and update tables exact date using the create_time or update_time in MySQL.At first, use the SHOW command. The syntax is as followsSHOW TABLE STATUS;We are considering our database ‘test3’, which is already having some tablesmysql> use test3; Database changedNow use the following query to display all the tables in the database test3mysql> show tables;The following is the output+-------------------------+ | Tables_in_test3         | +-------------------------+ | add6hour                | | deletedemo              | | differentdatetime       | | fieldlessthan5chars     | | ... Read More

Rename Root @ localhost username in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

657 Views

The syntax is as follows to rename Root @localhostUPDATE MySQL.user SET user = ‘yourNewRootName’ WHERE user = 'root';To understand the above concept, let us check all the user names and host. The query is as followsmysql> select user, host from MySQL.user;The following is the output+------------------+-----------+ | user             | host      | +------------------+-----------+ | Bob              | %         | | Manish           | %         | | User2            | %       ... Read More

Advertisements