• Image 1
  • Image 2
  • Image 3
  • Image 4
  • Image 5
  • Image 6
  • Image 7




Friday, September 21, 2012

HAVING AND SubSELECT IN MYSQL


Hello world… welcome to my blog at “We Are Knowledge”… In here you will getting many IT knowledge, like as : PHP, MYSQL, Visual Basic, PASCAL, and other program languages.. likely this article want discuss about “MYSQL”. You will know ?? let’s do read my article…

MYSQL is database.. And used for saving the data. But now, I want giving you IT knowledge about “HAVING and SubSELECT in MYSQL”…


1. HAVING

Having in mysql is query command provided by MYSQL for the number of items (types) for each record. Usually we are just using GROUP BY for classifying. we can use when having the same query command group by.


Example without “having”

SELECT hardware.id_hardware, COUNT(price.id_product) as  total
FROM hardware, price
WHERE hardware.id_hardware = price.id_product
AND total >= 5
GROUP BY hardware.id_hardware


To result :

| id_hardware  | total    |
|1                        | 1          |
|2                        |1           |
|3                        |1           |
|4                        |1           |
|5                        |1           |



Example with “having”


SELECT hardware.id_hardware, COUNT(price.id_product) as  total
FROM hardware, price
WHERE hardware.id_hardware = price.id_product
GROUP BY hardware.id_hardware
HAVING total >=5


To result :

| id_hardware     | total     |
|1                           | 7          |
|2                           |5           |
|3                           |5           |
|4                           |9           |
|5                           |6           |



1. SubSELECT

 SubSELECT is query command there is a command that allows select in the select command.


Example 3 :

SELECT [table name 1, table name 2]
FROM [database name 1, database name 2]
(
SELECT table name 1, table name 2]
FROM [database name 1, database name 2]
)
WHERE [condition] ;


Example 4 :

SELECT id_hardware , name_hardware, price_hardware
FROM hardaware
WHERE name_hardware =
(
SELECT price_hardware
FROM  hardware
WHERE price_hardware >= 2500
)
ORDER BY price_hardware;


To Result :

|id_hardware |name_hardware| price_hardware|
|3                      | MODEM            |2900                    |
|6                      |MEMORY          |3200                    |
|10                    |HARD DISK      |3250                    |
|11                    | VGA                  | 6000                   |



Ok.. just it..
I hope you understand…
See you later

No comments:

Post a Comment