there are 3 tables

CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10; CREATE TABLE IF NOT EXISTS `category_typeadverts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `id_category` int(11) NOT NULL, `id_type` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; CREATE TABLE IF NOT EXISTS `type_adverts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `type_title` varchar(64) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ; 

how to create a query in this way, or how to do it with the help of a handler to select all records from the type_adverts table and select id and title for each record (one element can contain several rows from the category table) from the category table, while the two tables are related in the third category_typeadverts

    1 answer 1

    Than the option didn’t fit just to fuck each other?

     SELECT ta.*, category.* FROM type_adverts ta JOIN category_typeadverts ct ON(ta.id = ct.id_type) JOIN category ON (category.id = ct.id_category); 

    http://sqlfiddle.com/#!9/f7e211/1

    • the fact that I needed to display the type_title field only 1 time and not as you have in the table - vkt
    • I solved this question with two different requests and connected two samples, of course, it didn’t do without a cycle, I don’t know how correct it is - vkt
    • @vkt can be in one. add group by on unique field. title in your case. and group_concat across a field that has multiple values ​​for a single title. values ​​will be in one line separated by commas. - Lexx918