Found 4219 Articles for MySQLi

Query in MySQL for string fields with a specific length?

Ankith Reddy
Updated on 25-Jun-2020 11:14:27

4K+ Views

To query for string fields with a specific length, use the char_length() or length() from MySQL.SyntaxThe syntax is as follows −Case 1 − Use of char_length()This can be used when we are taking length in a number of characters.The syntax −select *from yourTableName where char_length(yourColumnName)=anySpecificLengthValue;Case 2 − Use of length()This can be used when we are taking the length in bytes.The syntax −select *from yourTableName where length(yourColumnName)=anySpecificLengthValue;To understand the above concept, let us first create a table. The query to create a table is as follows −mysql> create table StringWithSpecificLength -> ( -> Id int, -> Name varchar(100), -> FavouriteLanguage ... Read More

Does MySQL Boolean “tinyint(1)” holds values up to 127?

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

659 Views

Let us learn some points about TINYINT type in MySQL −The TINYINT type takes 1 byte i.e. 8 bits.The TINYINT(N), where N indicates the display width you want.For example, TINYINT(1) can be used to display width which is 1.Let us learn about the minimum and maximum values −The maximum value for tinyint is= (2(8-1)-1) = 127 The minimum value for tinyint is = -(2(8-1)) = -128.The value will be between -128 to 127. This means TINYINT (1) does not affect the maximum and minimum value of tinyint.Let us check it −Firstly, create a table with a column set as TINYINT ... Read More

Fetching rows added in last hour with MySQL?

Chandu yadav
Updated on 25-Jun-2020 11:16:36

4K+ Views

You can use date-sub() and now() function from MySQL to fetch the rows added in last hour.SyntaxThe syntax is as follows −select *from yourTableName where yourDateTimeColumnName create table LastHourRecords -> ( -> Id int, -> Name varchar(100), -> Login datetime -> ); Query OK, 0 rows affected (0.67 sec)Insert records in the form of datetime using insert command. The query to insert record is as follows −mysql> insert into LastHourRecords values(1, 'John', ' 2018-12-19 10:00:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into LastHourRecords values(2, 'Carol', '2018-12-19 10:10:00'); Query OK, 1 row affected (0.15 sec) ... Read More

MySQL always returning the bit values as blank? How to get the original values?

Arjun Thakur
Updated on 25-Jun-2020 11:17:50

239 Views

To get the original value, use the following syntax −Syntaxselect yourBitColumnName+0 from yourTableName;The above syntax cast the bit column to an integer. To understand the above concept, let us create a table and check how the returning value is blank. We will also see how to get the original value.The query to create a table.mysql> create table BitDemo -> ( -> hasValidId bit not null -> ); Query OK, 0 rows affected (1.21 sec)Insert some records in the table using insert command. The query to insert records is as follows −mysql> insert into BitDemo values(1); Query OK, 1 row affected ... Read More

How can I simulate an array variable in MySQL?

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

2K+ Views

Instead of simulating an array variable, use temporary table in MySQL. The syntax is as follows −create temporary table if not exists yourTemporaryTableName select yourColumnName1, yourColumnName2, ......N from yourTableName where conditionTo understand the above syntax, let us first create a table. The query to create a table is as follows −mysql> create table SimulateArrayDemo -> ( -> Id int, -> FirstName varchar(100), -> LastName varchar(100 -> ) -> ); Query OK, 0 rows affected (1.25 sec)Insert some records in the table using ... Read More

Should I use COUNT(*) to get all the records in MySQL?

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

95 Views

Whenever you want all the values like not null for a column then use count(*). This is faster than using count() method.The syntax to use count(*) is as follows −select count(*) as anyVariableName from yourTableName;To understand the above concept, let us first create a table. The query to create a table is as follows −mysql> create table CountingDemo    -> (    -> BookId int    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into CountingDemo values(100); Query OK, 1 row affected (0.13 sec) ... Read More

How can I simulate a print statement in MySQL?

Chandu yadav
Updated on 29-Jun-2020 08:11:52

7K+ Views

To simulate a print statement in MySQL, you can use select statement. The syntax is as follows −SELECT ‘anyStringValue’ as ’ ‘;You can check the above syntax at the MySQL command line client.Case 1To print a string.mysql> select 'HELLO MYSQL' as ' ';Output+-------------+ |             | +-------------+ | HELLO MYSQL | +-------------+ 1 row in set (0.00 sec)Case 2a) To print integer, use the following query −mysql> select 23 as ' ';Output+----+ |    | +----+ | 23 | +----+ 1 row in set (0.00 sec)b) To print float or double type, use the following ... Read More

Get only the date in timestamp in MySQL?

Arjun Thakur
Updated on 25-Jun-2020 11:00:02

2K+ Views

In order to get the date from the timestamp, you can use DATE() function from MySQL.The syntax is as follows −SyntaxSELECT DATE(yourTimestampColumnName) as anyVariableName from yourTableName;To understand the above syntax, let us first create a table. The query to create a table is as follows −mysql> create table DateFromTimestamp -> ( -> ShippingDateTime timestamp -> ); Query OK, 0 rows affected (0.60 sec)Insert date and time for the column ShippingDateTime we created above.The query to insert record is as follows −mysql> insert into DateFromTimestamp values('2012-12-26 13:24:35'); Query OK, 1 row affected (0.14 sec) mysql> insert into DateFromTimestamp values('2013-11-26 14:36:40'); ... Read More

How to make a primary key start from 1000?

Ankith Reddy
Updated on 25-Jun-2020 11:00:44

2K+ Views

To make a primary key start from 1000, you need to alter your table and set to auto_increment with value 1000. The syntax is as follows −alter table yourTableName auto_increment=1000;To understand the above syntax, let us first create a table. The query to create a table is as follows −mysql> create table PrimaryKey1000Demo    -> (    -> ProductId int auto_increment,    -> PRIMARY KEY(ProductId)    -> ); Query OK, 0 rows affected (0.56 sec)Now here is the query that will update the primary key to start from 1000 −mysql> alter table PrimaryKey1000Demo auto_increment=1000; Query OK, 0 rows affected (0.20 ... Read More

MySQL datatype to store month and year only?

George John
Updated on 29-Jun-2020 08:12:26

5K+ Views

You need to store the date as the complete date time rather than storing only month and year. If you declare as a datetime then you can extract the month and year using MONTH() and YEAR() function from MySQL.The syntax is as follows −select MONTH(yourDateTimeColumnName) as anyVariableName1, YEAR(yourDateTimeColumnName) as anyVariableName2 from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table OnlyMonthandYear    -> (       -> DueDateTime datetime    -> ); Query OK, 0 rows affected (0.56 sec)Insert date in the table using insert command. ... Read More

Advertisements