Posts

DATE Functions tutorial in PHP

If we want to get current date then we can use below shown sql query : SELECT CURRENT_DATE();               OR       We can use SELECT CURDATE(); If we want to get current date with time then we can use below sql query : SELECT SYSDATE(); we can also use  SELECT NOW(); If we want to find out month from sql query then we can use below shown sql query : SELECT MONTH("2019-10-15 09:34:21") AS DATE;

DROP and TRUNCATE Table Tutorial in PHP

 DROP will remove Whole Table(including Structure of Table with data) and TRUNCATE will remove only Whole data of table but table structure will remain as it is. This is the major difference between DROP and TRUNCATE. DROP and TRUNCATE Syntax : DROP TABLE table_name; TRUNCATE TABLE table_name;

ALTER Command Tutorial in PHP

Features of MySQL ALTER Command : 1) Add Column in a Table 2) Changing Data Type of a Column 3) Change Column Name 4) Adding Constraints to a Column 5) Changing Column Position 6) Delete Column 7) Renaming Tables ALTER Syntax : 1) For Add Column : ALTER TABLE table_name ADD column_name datatype; 2) For Modify Column : ALTER TABLE table_name MODIFY column_name datatype; 3) For Delete Column : ALTER TABLE table_name DROP COLUMN column_name datatype; 4) For Rename Column : ALTER TABLE table_name CHANGE column_name New_name datatype; 5) For Rename Table : ALTER TABLE table_name RENAME new_table_name; If we want to Add Extra Column named "Email" in table named "students" then we can use below shown sql query : ALTER TABLE students ADD Email varchar(100); If we want to place "Email" column after "name" column then in "students" table then we can use below shown sql query : ALTER TABLE students MODIFY Email varchar(100) AFTER name; If we want ...

UNION and UNION ALL in PHP

 In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. In "lecturers" table, there are 6 columns named id, name, percentage, age, gender, id_of_personal. In "lecturers" table, column name "id" is PRIMARY KEY and column name "id_of_personal" is FOREIGN KEY. UNION and UNION ALL Syntax : SELECT column1, column2 FROM table1 UNION/UNION ALL SELECT column1, column2 FROM table2; RULES : 1) Each SELECT statement within UNION must have the same number of columns. 2) The columns must also have ...

SubQuery with EXISTS and NOT EXISTS in PHP

 SELECT with SubQuery Syntax : SELECT columns FROM table1 WHERE column = (SELECT columns FROM table2 WHERE condition); In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. If we want to select records from "personal" table who belong to Delhi from "city" table using SubQuery then we can use below shown sql query : SELECT name FROM personal WHERE city = (SELECT cid FROM city WHERE cityname = "Delhi"); If we want to select records from "personal" table who belong to Delhi and Agra from...

GROUP BY Clause and HAVING Clause in PHP

GROUP BY :  The GROUP BY clause is used in conjunction with the SELECT Statement and Aggregate functions to group rows together by common column values. SELECT with GROUP BY Syntax : SELECT columns FROM table_name WHERE condition GROUP BY column_name(s); (Here above "condition" is Optional in above query). In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. SELECT with GROUP BY with Two Tables Syntax : SELECT columns FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name WHERE condition GROUP B...

JOIN Multiple Tables in PHP

 If we want to join multiple tables and table names are "personal", "city" and "courses" then we can use below shown sql query :  In "personal" table, there are 6 columns named id, name, percentage, age, gender, city. In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY. In "courses" table, there are 2 columns named course_id and course_name. In "courses" table, column name "course_id" is PRIMARY KEY. In "city" table, there are 2 columns named cid and cityname. In "city" table, column name "cid" is PRIMARY KEY. If we want to join above three tables then we can use below shown sql query : SELECT * FROM personal INNER JOIN city ON personal.city = city.cid INNER JOIN courses ON personal.city = courses.course_id; If we want to apply alias or short name for table name then we can use below shown sql query : SELECT * FROM pers...