How to use Order by with Multiple columns in MySQL

The data is not displayed in any particular order when you simply fetch it from the MySQL table. For displaying data in some meaningful way ORDER BY clause is used.

This sorts your result on the basis of the column name specified in the clause within the SELECT query.

It allows us to –

  • Specify ordering in single or multiple columns
  • Define sort results in ASC or DESC format.

How to use Order by with Multiple columns in MySQL

Read more

Get Last insert id from MySQL Table with PHP

There are various approaches to selecting the last insert ID from the MySQL table:

  • Fetch the ID by selecting a row in descending order and storing the ID.
  • Retrieve the maximum value.
  • The query below provides the subsequent AUTO_INCREMENT value for the chosen table, useful for acquiring the last ID.
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'user'

For example, if your AUTO_INCREMENT column’s last value is 8, executing the query will return 9.

In PHP there is an inbuilt method that simply returns the last insert id according to the previous insert query.

Get Last insert id from MySQL Table with PHP

Read more

How to Concatenate Multiple columns in MySQL

In this tutorial, I show how you can concatenate multiple columns in MySQL.

You can simply do this programmatically by separately select fields from MySQL Table and store their values in the single variable after concat their values.

But you can make the above process a little simpler by concatenating the values while selecting rows from DataBase Table.

Let’s take a simple example –

You have two columns – firstname, lastname within your DataBase Table you want to show both the columns values in a single string form. In this case, you can use MySQL functions to combine the values of the columns.

There are two functions for doing this –

  • CONCAT
  • CONCAT_WS

Both functions work similarly but have little difference.

How to Concatenate Multiple columns in MySQL

Read more

MySQL – Convert Unix Timestamp to Date Time

The Unix Timestamp, while efficient for data storage, is not human-readable and appears as a set of integer numbers to the user. This lack of readability necessitates converting it into a more user-friendly format before displaying it to end-users.

If you don’t perform this conversion directly while selecting rows from the Database Table, you’ll need to handle it programmatically. For instance, in PHP, you can utilize the date() function for conversion as follows:

$timestamp = 1690768832;
echo "date time : ".date('d-M-Y H:i:s a',$timestamp);

The above code gives the following output:

date time : 31-Jul-2023 02:00:32 am

In this tutorial, we will explore how to convert Unix Timestamp values into a more readable Date Time format when selecting data from a MySQL Database Table. Let’s dive in and make the process of converting Unix Timestamps more accessible and comprehensible.

MySQL - Convert Unix Timestamp to Date Time

Read more