Thoughts of mine

I write some of my Ideas, Issues I faced in the development and writing skill improvement articles about my personal activities.

No comments

MySQL LEFT, RIGHT JOIN

MySQL joins are hard for beginners. At least for me when I was beginner.
I will try to explain the joins in the simplest possible way.
Join in MySQL is a query where you can join one or more tables.
For example we have two tables: products and buyers with the following structures.
Table products:
mysql> SELECT * FROM products;
+----+--------------+--------------+
| id | product_name | manufacturer |
+----+--------------+--------------+
|  1 | Shoes        | Company1     |
|  2 | Laptop       | Company2     |
|  3 | Monitor      | Company3     |
|  4 | DVD          | Company4     |
+----+--------------+--------------+
4 rows in set (0.00 sec)
Table buyers:
mysql> SELECT * FROM buyers;
+----+------+------------+----------+
| id | pid  | buyer_name | quantity |
+----+------+------------+----------+
|  1 |    1 | Steve      |        2 |
|  2 |    2 | John       |        1 |
|  3 |    3 | Larry      |        1 |
|  4 |    3 | Michael    |        5 |
|  5 | NULL | Steven     |     NULL |
+----+------+------------+----------+
5 rows in set (0.00 sec)

Left Join

mysql> SELECT buyer_name, quantity, product_name FROM buyers LEFT JOIN products ON
 buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve      |        2 | Shoes        |
| John       |        1 | Laptop       |
| Larry      |        1 | Monitor      |
| Michael    |        5 | Monitor      |
| Steven     |     NULL | NULL         |
+------------+----------+--------------+
5 rows in set (0.00 sec)
What happened?
Mysql starts with the left table (buyers). For each row from the table buyers mysql scans the table products, finds the id of the product and returns the product name. Then the product name is joined with the matching row from the table buyers. For unmatched rows it returns null.
To make it simpler, the above query is same as (except the unmatched rows are not returned):
mysql> SELECT buyers.buyer_name, buyers.quantity, products.product_name FROM buyer
s,products WHERE buyers.pid=products.id;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve      |        2 | Shoes        |
| John       |        1 | Laptop       |
| Larry      |        1 | Monitor      |
| Michael    |        5 | Monitor      |
+------------+----------+--------------+
4 rows in set (0.00 sec)

No comments :

Post a Comment

No comments

Ruby On Rails 'Include' and 'Extend' example


Module level class method can't be available to any class use 'Include' Or 'Extend', Only modules instance methods available


When use include keyword for include a module into class it make module level instance method as to class level instance method

When use extend keyword for extend a module into class it make module level instance method as to class level class method

Example code:


module Sample
def ins_mth; end
def self.cls_mth; end
end

class S1
include Sample
end
class S2
extend Sample
end

s1 = S1.new
s2 = S2.new


begin
S1.ins_mth
p 'Included modules instance method available as class Method'
rescue Exception => e
begin
s1.ins_mth
p 'Included modules instance method available as instance Method'
rescue Exception => e
p 'Included modules instance method does not available anywhere'
end
end

begin
S1.cls_mth
p 'Included modules class method available as class Method'
rescue Exception => e
begin
s1.cls_mth
p 'Included modules class method available as instance Method'
rescue Exception => e
p 'Included modules class method does not available anywhere'
end
end

begin
S2.ins_mth
p 'Extended modules instance method available as class Method'
rescue Exception => e
begin
s2.ins_mth
p 'Extended modules instance method available as instance Method'
rescue Exception => e
p 'Extended modules instance method does not available anywhere'
end
end

begin
S2.cls_mth
p 'Extended modules class method available as class Method'
rescue Exception => e
begin
s2.cls_mth
p 'Extended modules class method available as instance Method'
rescue Exception => e
p 'Extended modules class method does not available anywhere'
end
end

Out put of this code :

"Included modules instance method available as instance Method"
"Included modules class method does not available anywhere"
"Extended modules instance method available as class Method"
"Extended modules class method does not available anywhere"


No comments :

Post a Comment