简体   繁体   中英

How to fetch data by joining two table using condition in PHP and MySQL

I need one help. I need to fetch data by joining two table using some condition in PHP and Mysql. Here the conditions are little bit tricky.Let me to explain the two table first.

db_restaurant:

member_id      hotel_name     subcat_id     distance

   1              Maa           10          15


   2             Tprutee         4          1700


    3              Tarini        5          10

    4            Tasty           10         7


   5             sagar            5          21


   6             Magar            3          18


   7            bagar             5          20

   8            duat              4           5

db_subcat:

subcat_id          subcat_name

  3                  subcat1

  4                  subcat2

  5                  subcat3

  10                 subcat4

I need to join the above two table and fetch the subcat_id and subcat_name using some condition which is given below.

hints:

< 5 km=0
5-10 km=1
10-20 km=2
> 20 km=3

From the above it can be cleared that if distance is <5 KM then index is 0 ,distance is 5-10 KM index is 1 and so on . Here user has only the input $distance=0 or $distance=1 or $distance=2 or $distance=3 to fetch the required data.Overall my requirement is suppose user has input $distance=2 so all subcategory name and id should fetch within the distance 10-20 from the db_restaurant table and so on. Please help me.

That's quite easy to generate sql statements in PHP. Something like:

$sql = "select r.hotel, s.id, s.name
from db_subcat s
left join db_restaurant r on s.id = r.subcat
where r.distance";

switch ($hint)
{
    case 1:
        $sql .= " < 5";
        break;
    case 2:
        $sql .= " between 5 and 10";
        break;
    case 3:
        $sql .= " between 10 and 20";
        break;
    case 4:
        $sql .= " < 20";
        break;
}

$res = mysql_query($sql);

Or a pure SQL solution to the same problem:

DROP TABLE IF EXISTS `test`.`db_restaurant`;

CREATE TABLE  `test`.`db_restaurant` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `hotel` varchar(45) NOT NULL,
  `subcat` int(10) unsigned NOT NULL,
  `distance` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

insert into test.db_restaurant() values
(1,'Maa',10,15),
(2,'Tprutee',4,1700),
(3,'Tarini',5,10),
(4,'Tasty',10,7),
(5,'sagar',5,21),
(6,'Magar',3,18),
(7,'bagar',5,20),
(8,'duat',4,5);

DROP TABLE IF EXISTS `test`.`db_subcat`;

CREATE TABLE  `test`.`db_subcat` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

insert into test.db_subcat values
(3,'Subcat 1'),
(4,'Subcat 2'),
(5,'Subcat 3'),
(10,'Subcat 4');

DROP TABLE IF EXISTS `test`.`db_hint`;

CREATE TABLE  `test`.`db_hint` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `data` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

insert into test.db_hint values
(1,' < 5'),
(2,' between 5 and 10'),
(3,' between 10 and 20'),
(4,' > 20');

SELECT * FROM db_hint
where id=2;

select r.hotel, s.id, s.name
from db_subcat s
left join db_restaurant r on s.id = r.subcat
where r.distance > 20;

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`db_distance` $$
CREATE PROCEDURE `test`.`db_distance` (_hint int)
BEGIN

select data into @h from test.db_hint where id=_hint;

set @s =concat("select r.hotel, s.id, s.name
from db_subcat s
left join db_restaurant r on s.id = r.subcat
where r.distance",@h);

prepare stmt from @s;
execute stmt;
deallocate prepare stmt;

END $$

DELIMITER ;

call db_distance(2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM