Found 4219 Articles for MySQLi

Order by numeric value from string records separated by numbers like CSE 15, CSE 11, etc.?

AmitDiwan
Updated on 31-Dec-2019 07:45:16

69 Views

Let us first create a table −mysql> create table DemoTable1969    (    BranchCode varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1969 values('CSE 101'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 15'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 6'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1969 values('CSE 201'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Set a specific value for the first three column values in MySQL?

AmitDiwan
Updated on 31-Dec-2019 07:42:15

156 Views

To set a specific value for only 1st three values, you need to use LIMIT 3. Let us first create a table −mysql> create table DemoTable1968    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1968(Name) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('Sam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1968(Name) values('Mike'); Query OK, 1 ... Read More

Select and insert values with preceding zeros in a MySQL table

AmitDiwan
Updated on 31-Dec-2019 07:37:56

102 Views

For this, you can use INSERT INTO SELECT statement along with LPAD(). Let us first create a table −mysql> create table DemoTable1967    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserId varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1967(UserId)    select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Query OK, 1 row affected (0.00 sec) Records: 1  Duplicates: 0  Warnings: 0 mysql> insert into DemoTable1967(UserId)    select LPAD(COALESCE(MAX(id), 0) + 1, 3, '0') from DemoTable1967; Query OK, 1 row affected (0.00 sec) ... Read More

How to use if/else condition in select in MySQL?

AmitDiwan
Updated on 31-Dec-2019 07:36:35

1K+ Views

Let us first create a table −mysql> create table DemoTable1966    (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(20),    PhotoLiked int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1966(UserName, PhotoLiked) values('Chris', 57); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1966(UserName, PhotoLiked) values('David', 100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1966(UserName, PhotoLiked) values('Mike', 68); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1966(UserName, PhotoLiked) values('Sam', 78); Query OK, 1 row affected (0.00 sec)Display all ... Read More

How to identify a column with its existence in all the tables with MySQL?

AmitDiwan
Updated on 31-Dec-2019 07:34:29

99 Views

To identify a column name, use INFORMATION_SCHEMA.COLUMNS in MySQL. Here’s the syntax −select table_name, column_name from INFORMATION_SCHEMA.COLUMNS where table_schema = SCHEMA() andcolumn_name='anyColumnName';Let us implement the above query in order to identify a column with its existence in all tables. Here, we are finding the existence of column EmployeeAge −mysql> select table_name, column_name    FROM INFORMATION_SCHEMA.COLUMNS    WHERE table_schema = SCHEMA()    AND column_name='EmployeeAge';This will produce the following output displaying the tables with specific column “EmployeeAge” −+---------------+-------------+ | TABLE_NAME    | COLUMN_NAME | +---------------+-------------+ | demotable1153 | EmployeeAge | | demotable1297 | EmployeeAge | | demotable1303 | EmployeeAge | | demotable1328 ... Read More

Get maximum age from records with similar student names in MySQL

AmitDiwan
Updated on 31-Dec-2019 07:31:31

913 Views

For this, you can use GROUP BY along with aggregate function MAX(). Let us first create a table −mysql> create table DemoTable1964    (    StudentName varchar(20),    StudentAge int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1964 values('Chris', 23); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('David', 34); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('Chris', 27); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('Sam', 31); Query OK, 1 row affected (0.00 sec) mysql> ... Read More

MySQL query to create user and grant permission

AmitDiwan
Updated on 31-Dec-2019 07:29:43

340 Views

To create a user and grant permission, the syntax is as follows −create database yourDatabaseName DEFAULT CHARACTER SET utf8; create user `yourUserName` identified by yourPassword; GRANT SELECT ON yourDatabaseName .* TO `yourUserName`; GRANT INSERT ON yourDatabaseName .* TO `yourUserName`; GRANT UPDATE ON yourDatabaseName .* TO `yourUserName`; GRANT DELETE ON yourDatabaseName .* TO  `yourUserName`; GRANT EXECUTE ON yourDatabaseName .* TO `yourUserName`;Here is the query to create user and grant permission −mysql> create database demo_app DEFAULT CHARACTER SET utf8; Query OK, 1 row affected, 1 warning (0.00 sec) mysql> create user `John_123` identified by '123456'; Query OK, 0 rows affected (0.00 sec) ... Read More

MySQL query to separate and select string values (with hyphen) from one column to different columns

AmitDiwan
Updated on 31-Dec-2019 07:25:49

404 Views

For this, you can use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable1962    (    EmployeeInformation text    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1962 values('101-John-29'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1962 values('102-David-35'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1962 values('103-Chris-28'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1962;This will produce the following output −+---------------------+ | EmployeeInformation | +---------------------+ | 101-John-29   ... Read More

Parse a string to get a number from a large string separated by underscore

AmitDiwan
Updated on 31-Dec-2019 07:22:24

76 Views

Let us first create a table −mysql> create table DemoTable1961    (    Title text    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1961 values('You_can_remove_the_string_part_only-10001-But_You_can_not_remove_the_numeric_parts'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1961;This will produce the following output −+------------------------------------------------------------------------------------+ | Title                                                                       ... Read More

A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?

AmitDiwan
Updated on 31-Dec-2019 07:19:50

282 Views

For this, you can use GROUP_CONCAT(). Use SUM() to add the User Id. Let us first create a table −mysql> create table DemoTable1960    (    StudentId int,    StudentName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1960 values(100, 'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1960 values(101, 'Bob'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1960 values(102, 'David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1960 values(103, 'Mike'); Query OK, 1 row affected (0.00 sec)Display ... Read More

Advertisements