mysql - Subqueries and Union -


i have next mysql tables

      create table if not exists `my_app`.`hotel` (       `id` int not null auto_increment,       `name` varchar(45) not null,       `destination_id` int not null,       primary key (`id`),       index `idx_name` (`name` asc),       index `fk_hotel_destination1_idx` (`destination_id` asc),       constraint `fk_hotel_destination1`         foreign key (`destination_id`)         references `my_app`.`destination` (`id`)         on delete no action         on update no action)     engine = innodb  
      create table if not exists `my_app`.`hotel_alias` (       `id` int not null auto_increment,       `hotel_id` int not null,       `name` varchar(45) not null,       primary key (`id`),       index `idx_name` (`name` asc),       index `fk_hotel_alias_hotel_idx` (`hotel_id` asc),       constraint `fk_hotel_alias_hotel`         foreign key (`hotel_id`)         references `my_app`.`hotel` (`id`)         on delete no action         on update no action)     engine = innodb  
      create table if not exists `my_app`.`destination` (       `id` int not null auto_increment,       `name` varchar(45) not null,       primary key (`id`),       index `idx_name` (`name` asc))     engine = innodb  
      create table if not exists `my_app`.`place` (       `id` int not null auto_increment,       `name` varchar(45) not null,       `destination_id` int not null,       primary key (`id`),       index `idx_name` (`name` asc),       index `fk_place_destination1_idx` (`destination_id` asc),       constraint `fk_place_destination1`         foreign key (`destination_id`)         references `my_app`.`destination` (`id`)         on delete no action         on update no action)     engine = innodb  

i want create view populate jquery autocomplete made next query

      select name, 'hotel' type, (select id destination hotel.destination_id = destination.id) destination_id hotel      union       select name, 'place' type, (select id destination place.destination_id = destination.id) destination_id place     union      select name, 'alias' type, (select destination.id destination,hotel hotel_alias.hotel_id = hotel.id , hotel.destination_id = destination.id) destination_id hotel_alias  

which returns following results

      |name                       |type   |destination_id     ---------------------------------------------------      |hotel casa maya            |hotel  |1     |sandos caracol             |hotel  |2     |cabaƱas tulum              |hotel  |3     |sandos luxury              |hotel  |1     |ocean spa                  |hotel  |1     |sandos playacar            |hotel  |2     |walmart                    |place  |1     |walmart                    |place  |2     |centro                     |place  |3     |campo de golf pok-ta-pok   |place  |1     |sandos beach scape         |alias  |2     |sunset spa                 |alias  |1  

the results correct, wonder if im using "subqueries" , "union" correctly or if there better way generate same result optimizing query?

gracias!

you make use of join

have at: http://dev.mysql.com/doc/refman/4.1/en/join.html


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -