Wednesday, January 18, 2012

Select the select query (double select)


Running SELECT query in the SELECT will need to do when we want to retrieve database information let the results more quickly. The conventional way which is often used is to query 2 times so that the processing will take longer.
Well as an example, we will create a table in a database table that province, which contains the name of the province and population.

CREATE TABLE `propinsi` (
`nama_propinsi` varchar(100) collate latin1_general_ci NOT NULL,
`jumlah_penduduk` int(20) unsigned NOT NULL,
PRIMARY KEY  (`nama_propinsi`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

INSERT INTO `propinsi` VALUES ('Jatim', 100000);
INSERT INTO `propinsi` VALUES ('Jateng', 200000);
INSERT INTO `propinsi` VALUES ('Jabar', 300000);
INSERT INTO `propinsi` VALUES ('DKI Jakarta', 400000);


Once the table is formed, we will create a query to display the name of the province whose total population is larger than the population of the province jateng.
Logic generally we find first the total population (n) jateng province, and then we are looking for a larger population of n earlier. 2 query's happening ..
In this case, we will use a SELECT query in the select:

select nama_propinsi from propinsi where jumlah_penduduk >
(select jumlah_penduduk from propinsi where nama_propinsi='Jateng');


The output of the query are the provinces of West Java and Jakarta, which means that provincial population is larger than the population of the province of West Java and Central Java is Jakarta.

So, select the SELECT query is enough to use when we need a filter from another table in one database

Well it is quite simple.

Hopefully this information useful.


Don't miss it
vote web ini yaa klik disini!!

No comments:

Post a Comment