MySQLi Articles

Page 11 of 341

MySQL AUTO_INCREMENT with Examples

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 521 Views

Let us understand how ATUO_INCREMENT works −The AUTO_INCREMENT attribute is used to generate a unique identify for new rows. Let us see how this statement works. Before that, consider the below query −QueryCREATE TABLE tableName (    id MEDIUMINT NOT NULL AUTO_INCREMENT,    name CHAR(30) NOT NULL,    PRIMARY KEY (id) ); INSERT INTO tableName (name) VALUES (‘val1’), ('val2'), ('val3'), ('val4'); SELECT * FROM tableName;Output+----+---------+ | id | name | +----+---------+ | 1  | val1 | | 2  | val2 | | 3  | val3 | | ...

Read More

SQL queries for counter web visits per day, month, year and totals

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 617 Views

Let us understand how to form the query to find the number of web visits per day, per month, per year, and the total in MySQL:Note: We assume we have created a database named ‘DBNAME’ and a table named ‘tableName’.Let us see the MySQL query which can be used to get the web visits per day, month, year and totals −QuerySELECT COUNT(DISTINCT ip) FROM tableName WHERE create_at >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH AND create_at < LAST_DAY(NOW()) + INTERVAL 1 DAYThe above query searches through a range of DATETIME values by beginning from the present month ...

Read More

How can I count visitors per page per day using MySQL?

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 479 Views

Note: We assume we have created a database named ‘DBNAME’ and a table named ‘tableName’.Let us understand how the count of visitors per day per page can be queried using MySQL. This can be done using the bit group function −QuerySELECT DATE(date) Date, page_id, COUNT(*) colName FROM tableName GROUP BY DATE(date), page_idHere ‘colName’ refers to the ‘visits per day’ column, and ‘tableName’ refers to the table that contains details about visitors.It makes sure that the duplicate values in the table are removed when the above query is run.

Read More

Making slow queries fast using composite indexes in MySQL

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 624 Views

Let us first see what is Composite Index −A composite index is an index that is used on multiple columns.It is also known as a multiple-column index.MySQL allows the user to create a composite index which can consist of up to 16 columns.The query optimizer uses the composite indexes for queries which will test all columns in the index.It can also be used for queries which will test the first columns, the first two columns, and so on.If the columns are specified in the right order in the index definition, a single composite index can be used that would speed ...

Read More

MySQL Composite Index

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 9K+ Views

A composite index is an index that is used on multiple columns. It is also known as a multiplecolumn index.FeaturesLet us see the features −MySQL allows the user to create a composite index which can consist of up to 16 columns.The query optimizer uses the composite indexes for queries which will test all columns in the index.It can also be used for queries which will test the first columns, the first two columns, and so on.If the columns are specified in the right order in the index definition, a single composite index can be used that would speed up certain ...

Read More

How to search multiple columns in MySQL?

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 2K+ Views

Let us understand how to search multiple columns in MySQL −Note: We assume we have created a database named ‘DBNAME’ and a table named ‘tableName’.The ‘AND’ and ‘OR’ operators can be used, depending on what the user wants the search to return.Let us see that with the help of an example −ExampleSELECT colName FROM tableName WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;In the above example, the ‘AND’ operator is used.This means that both the clause has to match a record for the result to be returned.QuerySELECT colName FROM tableName WHERE my_col LIKE %$param1% OR another_col LIKE %$param2%;In the above ...

Read More

Searching on Two Keys in MySQL

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 211 Views

Let us understand how to search on two keys in MySQLSearching on two keys can be achieved using the ‘OR’ with the help of single key which is well optimized or using ‘AND’ which is well optimized. Let us see how searching on two different keys can be done combining it with ‘OR’ operation −SELECT field1_index, field2_index FROM tableName WHERE field1_index = '1' OR field2_index = '1'This is an optimized version of the query. It can also be done efficiently using the ‘UNION’ that combines the output of two separate ‘SELECT’ statements. Every ‘SELECT’ statement searches only for one key ...

Read More

Using User-Defined Variables in MySQL

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 423 Views

Let us understand what user variables are and how they can be used in MySQL. We will also see the rules −User variables are written as @var_name. Here, the ‘var_name’ refers to variable name, which consists of alphanumeric characters, ., _, and $.A user variable name can contain other characters if they are quoted as a string or identifier.User-defined variables are session specific.A user variable which is defined by one client can’t be seen or used by other clients.But the only exception is that if a user has access to the Performance Schema user_variables_by_thread table, then that user can see ...

Read More

The Rows Holding the Group-wise Maximum of a Certain Column in MySQL

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 463 Views

Let us understand how to find the rows that hold the group wise maximum of a specific column in MySQL −The syntax to find the rows that hold the group-wise maximum of a specific column in MySQL is as follows −SELECT colName1, colName2, colName3 FROM tableName s1 WHERE colName3=(SELECT MAX(s2. colName3) FROM tableName s2 WHERE s1. colName1= s2. colName1) ORDER BY colName1;Let’s say we have the following PRODUCT Table −+---------+----------+--------+ | Article | Warehouse| Price  | +---------+----------+--------+ | 1       | North    | 255.50 | | 1       | North    | 256.05 | | ...

Read More

Maximum of Column per Group in MySQL

AmitDiwan
AmitDiwan
Updated on 09-Mar-2021 167 Views

Let us understand how to find the maximum of a column per group in MySQL −SELECT colName1, MAX(colName2) FROM tableName GROUP BY colName1 ORDER BY colName1;We will now see a live example. Let’s say we have a table PRODUCT −+---------+--------+ | Article | Price  | +---------+--------+ | 1       | 255.50 | | 1       | 256.05 | | 2       | 90.50  | | 3       | 120.50 | | 3       | 123.10 | | 3       | 122.10 | +---------+--------+Following is the query to get the maximum of column per group −QuerySELECT Article, MAX(Price) AS MaxPrice FROM Product GROUP BY Article ORDER BY Article;Output+--------------+--------------+ | Article      | MaxPrice | +--------------+--------------+ | 0001         | 256.05 | | 0002         | 90.50 | | 0003 | 123.10 | +--------------+--------------+

Read More
Showing 101–110 of 3,404 articles
« Prev 1 9 10 11 12 13 341 Next »
Advertisements