Hi,I'm new to PHP...I currently doing a PHP project....I don't understand this code..can someone please kindly explain to me..Thanks
select * from category c left join category2 c2 on c2.cat1_id=c.cat_id where c.cat_id=" . $_GET['cate1'] . " and c2.cat2_id=" . $_GET['cate2']
What does the c & c2 do?
& what is the output result?
Here the database table:
CREATE TABLE `category` (
`cat_id` bigint(20) NOT NULL auto_increment,
`cat_name` varchar(200) NOT NULL default '',
PRIMARY KEY (`cat_id`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;
CREATE TABLE `category2` (
`cat2_id` bigint(20) NOT NULL auto_increment,
`cat1_id` bigint(20) NOT NULL default '0',
`cat2_name` varchar(200) NOT NULL default '',
PRIMARY KEY (`cat2_id`)
) TYPE=MyISAM AUTO_INCREMENT=5 ;
The c and c2 that you can see are "aliases". An alias is just an alternative name for something. In the case of your statement, c and c2 are the shorthand aliases given to the tables category and category2. This is done to make the statements shorter. It just makes it easier to type / follow. You don't have to constantly type out long table names (and in some queries, you might need to type them out many times!!)
Note, you only need to use aliases on queries that call more than one table, so you can differentiate between the different fields between the tables.
Eg, if both tables have an 'id' field, you would do "SELECT c.id, c2.id FROM category AS c, category2 AS c2".
It's also good practice to use the AS keyword to state aliases, although it's not compulsary, it makes more sense.
You don't seem to know much about the code... is this a school assignment or homework?
A good place to start learning sql... http://w3schools.com/sql/