MySQL GROUP BY
The GROUP BY statement in SQL is used to arrange data into groups with the help of some functions. GROUP BY statement is used to retrieve grouped records based on one or more columns.
The syntax for GROUP BY Statement in SQL -:
Description MySQL GROUP BY -:
GROUP BY(Nested Grouping) using more than one column -
Syntex -
GROUP BY Condition -
Syntex -
An aggregate function performs a calculation on multiple values and returns a single value.
1. MAX ()
MAX function returns the maximum value of an expression.
2. MIN ()
MIN function returns the minimum value of an expression.
3. AVG ()
AVG function returns the average value of an expression.
4. SUM ()
SUM function returns the summed value of an expression.
5. COUNT () and COUNT (*)
COUNT function returns the count of an expression.
For preforming the operations on dates, MySQL has provided some function:
1. NOW ()
NOW function returns the current date and time.
2. DATE ()
3. MONTH ()
MONTH function returns the month portion of a date value.
4. MONTHNAME ()
MONTHNAME function returns the full name of the month for a date.
5. YEAR ()
YEAR function returns the year portion of a date value.
6. DAY ()
DAY function returns the day portion of a date value.
7. DAYNAME ()
For preforming the operations on strings, MySQL has provided some function:
1. UCASE ()/UPPER ()
It converts all characters in the specified string to uppercase. If there are characters in the string that are not letters, they are unaffected by this function.
2. LCASE ()/LOWER ()
3. MID ()/SUBSTRING ()/SUBSTR ()
This function allows you to extract a substring from a string.
4. LENGTH ()
5. LEFT ()
LEFT function allows you to extract a substring from a string, starting from the left-most character.
6. RIGHT ()
The RIGHT function allows you to extract a substring from a string, starting from the right-most character.
7. INSTR ()
INSTR function returns the location of a substring in a string.
8. LTRIM ()
LTRIM function removes all space characters from the left-hand side of a string.
9. RTRIM ()
RTRIM function removes all space characters from the right-hand side of a string.
10. TRIM ()
TRIM function removes all specified characters either from the beginning or the end of a string.
MySQL provides different Mathematics functions to perform Math operations. In your syllabus we have to learn these three functions:
1. POWER ( ) or POW( )
2. ROUND ( )
3. MOD ( )
Prerequisite
- To know how to install Python: Click Here
- To know how to install MySQL watch the following Video:
STEP-1: Start Python
STEP-2: install and import mysql-connector
- The next step is to install mysql-connector. mysql-connector is available for Python that is used for connecting the python program with the MySQL Database.
- This connector can be installed in python using the following command (run it in cmd): pip install mysql-connector
- If you are facing any error while using this command click here to resolve your environment Path related error. After that, you will not face any problem.
- After installing the mysql-connector, import it in your program by using the following command: import mysql.connectororimport mysql.connector as sql
STEP-3: Connect Python with MySQL database using connect()
- connect() function is used to make the connection from python to MySQL and returns a connection object.
- It is available inside mysql.connector module. Hence to use this function we have to import mysql.connector.
- Prototype of connect( ) Function:
<conn_obj> = <mysql.connector>.connect(host="localhost", user="yourusername", password="yourpassword", database ="test“) - You can check for successful connection using is_connected( ) Function with connected Object.
if <conn_object>.is_connected()==False: print("Not Conneted")
STEP-4: Create a Cursor instance using cursor()
STEP-5: Execute SQL Query by Using execute( ) Function
STEP-6: Close the Connection
Full code for Python-MySQL Connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ##MYSQL connection with python import mysql.connector as sql con = sql.connect(host='127.0.0.1',user='root',passwd='password') if con.is_connected() == True: mycursor = con.cursor() mycursor.execute("create database student") mycursor.execute("use student") mycursor.execute("create table xiicom (name varchar(20))") for i in range(5): name = input("Enter a Name:") s3 = "insert into xiicom values ('{}')".format(name) mycursor.execute(s3) con.commit() con.close() else: print("Not Connected") |
Follow the below steps to remove the error:
Step 1: Go To Start and Search MySQLStep 2: Inside MySQL click on MySQL Installer Community.
Step 3: Now click on Reconfigure on MySQL Server
Step 4: In the High Availability Menu click on "standalone MySQL Server / Classic MySQL Replication" and click Next
Step 5: Click Next on Type and Networking Menu
Step 6: Now on Authentication Method choose Use Legacy Authentication Method (Retain MySQL 5.x Compatibility) and click Next
Step 8: ITS DONE. NOW CHECK YOUR PYTHON CODE. IT WILL NOT HAVE ANY ERROR.