thara Posted May 20, 2012 Share Posted May 20, 2012 I have 6 tables and need to retrieve some data at once from those tables. can any body tell me, can I select my values from one query. These values I needed. institute_id institute_name city_name options (relate to institute_id) mediums (relate to institute_Id) These are from my tables CREATE TABLE institutes ( institute_id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT, login_id INT(4) UNSIGNED NOT NULL, address_id INT(4) NOT NULL, contact_id INT(4) NOT NULL, institute_code INT(4) NOT NULL, institute_name VARCHAR(80) NOT NULL, institute_details VARCHAR(80) NOT NULL, ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE address ( address_id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT, address_one VARCHAR(40) NOT NULL, address_two VARCHAR(40) DEFAULT NULL, city_id INT(4) UNSIGNED NOT NULL, PRIMARY KEY (address_id), ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE city( city_id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT, city_name VARCHAR(30) NOT NULL, state_id INT(2) UNSIGNED NOT NULL, PRIMARY KEY (city_id), ) ENGINE=MyISAM; CREATE TABLE medium( medium_id INT(2) UNSIGNED NOT NULL AUTO_INCREMENT, medium_name VARCHAR(30) NOT NULL, PRIMARY KEY (medium_id), ) ENGINE=MyISAM; CREATE TABLE options( option_id INT(2) UNSIGNED NOT NULL AUTO_INCREMENT, option_name VARCHAR(60) NOT NULL, PRIMARY KEY (option_id), ) ENGINE=MyISAM; CREATE TABLE institute_medium( im_id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT, medium_id INT(2) UNSIGNED NOT NULL, institute_id INT(4) UNSIGNED NOT NULL, PRIMARY KEY (im_id) ) ENGINE=MyISAM; CREATE TABLE institute_option( io_id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT, option_id INT(2) UNSIGNED NOT NULL, institute_id INT(4) UNSIGNED NOT NULL, PRIMARY KEY (io_id) ) ENGINE=MyISAM; those are my tables I tried like this but Im confuse when Im try to get options and mediums.. there are always more than one option and medium in instutite_medium to one institute... $q = "SELECT institutes.institute_id, institute_name, city_name, DATE_FORMAT(registration_date, '%M %e, %Y') AS date, GROUP_CONCAT( medium_name SEPARATOR ', ') AS mediums FROM institutes INNER JOIN address ON institutes.address_id = address.address_id INNER JOIN city ON address.city_id = city.city_id inner join institute_medium on institute_medium.medium_id = medium.medium_id right join institutes on institutes.institute_id = institute_medium.institute_id ORDER BY registration_date DESC"; but I cant get values... any comments are greatly appreciated. thank you... Quote Link to comment https://forums.phpfreaks.com/topic/262813-select-query-from-multiple-tables/ Share on other sites More sharing options...
Barand Posted May 20, 2012 Share Posted May 20, 2012 ... FROM institutes ... RIGHT JOIN institutes ... ??? Quote Link to comment https://forums.phpfreaks.com/topic/262813-select-query-from-multiple-tables/#findComment-1346991 Share on other sites More sharing options...
thara Posted May 20, 2012 Author Share Posted May 20, 2012 thanks for response.. Actually I have any Idea about join query. I just use right join for my experiments... can u explain how its work... any comments are greatly appreciated. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/262813-select-query-from-multiple-tables/#findComment-1346993 Share on other sites More sharing options...
fenway Posted May 20, 2012 Share Posted May 20, 2012 Show us what output you're expecting. Quote Link to comment https://forums.phpfreaks.com/topic/262813-select-query-from-multiple-tables/#findComment-1347027 Share on other sites More sharing options...
thara Posted May 21, 2012 Author Share Posted May 21, 2012 I want to display like this "institute name" - "city name" you have add you profile under "option1, option2, option3, and so forth" and teaching "medium1, medium2, etc" medium classes. "registration date" I need to display every registered institute on my home page like that....... thans you.. Quote Link to comment https://forums.phpfreaks.com/topic/262813-select-query-from-multiple-tables/#findComment-1347186 Share on other sites More sharing options...
fenway Posted May 21, 2012 Share Posted May 21, 2012 Well, if you want to be lazy, you can use GROUP_CONCATS. Quote Link to comment https://forums.phpfreaks.com/topic/262813-select-query-from-multiple-tables/#findComment-1347442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.