How to Select Multiple Columns in MySQL with 5 Examples

For example : select multiple columns mysql, this query selects 2 columns, member_name and join_date, from the members table:

SELECT member_name, join_date
FROM members;

mysql select all columns and specific fields as

SELECT companyInfo.*, column20 AS Members, column11 FROM companyInfo
-- OR
SELECT column11, column20 AS Members, companyInfo.* FROM companyInfo

how to select multiple columns from different tables in mysql?

-- MySQL
-- t1 = table1
-- mn5 = column of table
SELECT t1.mn5, t2.dt4, t2.dt5, t2.dt3 #get dt3 data from table2
FROM table1 t1, table2 t2 -- Doesn't need to have t1, or t2
WHERE t1.mn5 = 'jamk' AND t2.dt4 = 'dhara' AND t2.dt5 = 898

mysql query select more columns

mysql> SELECT name, join_date FROM members;
+----------+------------+
| name | join_date |
+----------+------------+
| Vishal | 2021-02-04 |
| Dhaar | 2022-03-17 |
| Reema | 2020-05-13 |
| Ravi | 2021-08-27 |
| Krishn | 2015-08-31 |
| Chirpy | 2018-09-11 |
| Hanumanj | 2005-12-09 |
| Dave | 2004-04-29 |
| Fourtsnd | 2003-03-30 |
+----------+------------+

SQL SELECT with DISTINCT on multiple columns

mysql select multiple columns Examples

Sample SELECT statement

SELECT product_code, product_price, gst_no, product_no
FROM fruits
WHERE product_code='P0054';

SELECT with DISTINCT on two columns

SELECT DISTINCT product_code,product_price
FROM fruits
WHERE product_code='P0054';

SELECT with DISTINCT on three columns

SELECT DISTINCT product_code, product_price,gst_no
FROM fruits
WHERE product_code='P0054';

SELECT with DISTINCT on all columns of the first query

SELECT DISTINCT product_code,product_price,gst_no,product_no
FROM fruits
WHERE product_code='P0054';

SELECT with DISTINCT on multiple columns and ORDER BY clause

SELECT DISTINCT product_code,product_price
FROM fruits
WHERE product_code='P0054'
ORDER BY product_price;

COUNT() function and SELECT with DISTINCT on multiple columns

SELECT COUNT(*)
FROM (
SELECT DISTINCT product_code, product_price,gst_no
FROM fruits
WHERE product_code='P0054');

Selecting multiple columns/fields in MySQL subquery

SELECT DATE_FORMAT(fruitDate,'%M %Y') AS fruitMonth, COUNT(fruitID) AS Totalfruits, SUM(fruitTotal) AS TotalAmount, Z.fruitMemberFK, Z.MemberName, z.fruitTotal as MemberTotal
FROM fruits
INNER JOIN (SELECT DATE_FORMAT(fruitDate,'%M %Y') as Mon, fruitMemberFK, MemberName, SUM(fruitTotal) as fruitTotal
FROM fruits
GROUP BY DATE_FORMAT(fruitDate,'%M %Y'), fruitMemberFK, MemberName ORDER BY SUM(fruitTotal) DESC LIMIT 1) Z
ON Z.Mon = DATE_FORMAT(fruitDate,'%M %Y')
GROUP BY DATE_FORMAT(fruitDate,'%m%y'), Z.fruitMemberFK, Z.MemberName
ORDER BY DATE_FORMAT(fruitDate,'%y%m') DESC

I hope you get an idea about mysql select multiple columns.


#mysql  #sql  #database 

How to Select Multiple Columns in MySQL with 5 Examples
1.10 GEEK