Found 4378 Articles for MySQL

Using backticks in CONTACT() gives an error in MySQL

AmitDiwan
Updated on 08-Nov-2019 10:35:10

88 Views

Do not use backticks, you can use single quotes in CONCAT(). Following is the syntax −select concat(yourColumnName1, ' ', yourColumnName2) from yourTableName;Let us first create a table −mysql> create table DemoTable1359     -> (     -> Id int,     -> Name varchar(20)     -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1359 values(101, 'Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1359 values(102, 'Adam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1359 values(103, 'Mike'); Query OK, 1 row ... Read More

Sum values in MySQL from similar day records

AmitDiwan
Updated on 05-Nov-2019 10:19:27

694 Views

Use GROUP BY and DATE() for this. Let us first create a table −mysql> create table DemoTable1358     -> (     -> PurchaseDate datetime,     -> ProductPrice int     -> ); Query OK, 0 rows affected (1.59 sec)Insert some records in the table using insert command. Here, we have inserted date records, with some records with similar dates −mysql> insert into DemoTable1358 values('2019-09-20 12:34:00', 450); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1358 values('2019-09-21 11:00:00', 1050); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1358 values('2018-09-21 02:10:00', 2050); Query OK, 1 ... Read More

Display the result with not null value first and then with null value in MySQL

AmitDiwan
Updated on 05-Nov-2019 10:18:06

274 Views

Let us first create a table −mysql> create table DemoTable1357     -> (     -> StudentName varchar(40),     -> StudentCountryName varchar(30)     -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1357 values('Chris', 'US'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1357 values('David', NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1357 values('David', 'AUS'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1357 values('Carol', NULL); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1357 values('Mike', ... Read More

Alter a table column from VARCHAR to NULL in MySQL

AmitDiwan
Updated on 05-Nov-2019 10:15:48

409 Views

To alter, use the ALTER command with CHANGE as in the below syntax −alter table yourTableName change yourColumnName yourColumnName datatype NULL DEFAULT NULL;Let us first create a table −mysql> create table DemoTable1356     -> (     -> FirstName varchar(30)     -> ); Query OK, 0 rows affected (0.56 sec)Let us implement the above syntax to alter a table column to NULL −mysql> alter table DemoTable1356 change FirstName FirstName varchar(30) NULL DEFAULT NULL; Query OK, 0 rows affected (0.17 sec) Records: 0  Duplicates: 0  Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable1356 ... Read More

MySQL query to return all items in a single row

AmitDiwan
Updated on 05-Nov-2019 10:14:31

859 Views

For this, use GROUP_CONCAT(). Let us first create a table−mysql> create table DemoTable1355     -> (     -> Location text     -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1355 values('E:'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1355 values('AllPrograms'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1355 values('ChatApplicationInJava'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1355 values('MainFolder'); Query OK, 1 row affected (0.23 sec)Display all records from the table using select statement −mysql> select * ... Read More

Fix Error 1136: Column count doesn't match value count at row 1?

AmitDiwan
Updated on 05-Nov-2019 10:13:16

2K+ Views

You may get tis value, if you are missing the value for auto_increment column. The error is as follows −mysql> insert into DemoTable1353 values('Chris', 23); ERROR 1136 (21S01): Column count doesn't match value count at row 1You need to provide the value for auto_increment or leave it to automatic generation.Let us see an example and create a table −mysql> create table DemoTable1353     -> (     -> Id int NOT NULL AUTO_INCREMENT,     -> Name varchar(20),     -> Age int,     -> PRIMARY KEY(Id)     -> ); Query OK, 0 rows affected (0.52 sec)Insert ... Read More

Cast one of the values in MySQL and perform division with the other?

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

122 Views

For this, at first, use CAST(). Let us first create a table −mysql> create table DemoTable1352     -> (     -> Value1 int,     -> Value2 int     -> ); Query OK, 0 rows affected (0.54 sec)Insert some records in the table using insert command−mysql> insert into DemoTable1352 values(10, 30); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable1352 values(40, 60); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1352 values(110, 130); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement−mysql> select * from DemoTable1352; This ... Read More

Display only the duplicate column names appearing atleast thrice in MySQL

AmitDiwan
Updated on 05-Nov-2019 09:33:43

60 Views

Use HAVING COUNT() for this. Let us first create a table −mysql> create table DemoTable1351     -> (     -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,     -> StudentName varchar(40)     -> ); Query OK, 0 rows affected (1.08 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1351(StudentName) values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('David'); Query OK, 1 row affected (0.11 ... Read More

How to find all tables that contains columnA and columnB in MySQL?

AmitDiwan
Updated on 08-Jul-2020 09:16:49

110 Views

To find specific column names, use information_schema.columns Here, I am using Id in place of columnA and Name in place of columnB −mysql> select table_name as TableNameFromWebDatabase    -> from information_schema.columns    -> where column_name IN ('Id', 'Name')    -> group by table_name    -> having count(*) = 3;This will produce the following output. Following are the table with columns Id and Name −+--------------------------+ | TableNameFromWebDatabase | +--------------------------+ | student                  | | distinctdemo             | | secondtable              | | groupconcatenatedemo   ... Read More

Avoiding rewrite attributes with MySQL AUTO_INCREMENT

AmitDiwan
Updated on 05-Nov-2019 09:29:46

61 Views

You can avoid with the help of providing the value (NULL, 0, DEFAULT) with AUTO_INCREMENT. Let us first create a table −mysql> create table DemoTable1350    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1350 values(NULL, 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1350 values(0, 'Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1350 values(DEFAULT, 'Chris'); Query OK, 1 row affected (0.10 sec)Display all records ... Read More

Advertisements