Mysql With Right Join and Count

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Mysql With right Join And Count(), Distinct:

As we have two tables with tuples student (roll_no,name, course, age,


city), teacher(teacher_id, teacher_name, address)
Distinct:
select distinct name, age, city, address from student left join
teacher on roll_no=teacher_id group by name;
Count():
And now we want to count names in join tables, for that we use
following syntax—

select name, age, address, count(name) from student right join teacher
on roll_no=teacher_id group by name;
check this syntax by changing left/right joins and also by removing group
by clause.

Duplicate a Table in MySQL


CREATE TABLE new_table AS SELECT * FROM original_table;
Note:
new_tables inherits ONLY the basic column definitions, null settings
and default values of the original_table. It does not inherit indexes and
auto_increment definitions.
For that first use syntax:
CREATE TABLE new_table LIKE original_table;
Then insert values in it using syntax:
INSERT INTO new_table SELECT * FROM original_table;

To set the values of a column a primary key, use

alter table table_name add primary key (column_name you want to set
as primary key);

then use
update table_name set column_name=value where condition;

it will work.

JOINs
Here are the different types of the JOINs

 (INNER) JOIN: Returns records that have matching values in both


tables
 LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
 RIGHT (OUTER) JOIN: Returns all records from the right table, and
the matched records from the left table
 FULL (OUTER) JOIN: Returns all records when there is a match in
either left or right table
    

   

You might also like