简体   繁体   English

在php mysql中加入表

[英]Table Join in php mysql

I want to join all the tables, in all tables all columns names are same. 我想加入所有表,在所有表中所有列的名称都相同。 My query is below. 我的查询如下。 Please help me to fetch the data. 请帮助我获取数据。 My table structure is: 我的表结构是:

CREATE TABLE IF NOT EXISTS `play_school` (
  `token_id` varchar(255) NOT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ad_id` varchar(255) NOT NULL,
  `title` text NOT NULL,
  `category` varchar(255) NOT NULL,
  `name` text NOT NULL,
  `image` varchar(255) NOT NULL,
  `content` text NOT NULL,
  `offer` text NOT NULL,
  `note` text NOT NULL,
  `price` varchar(255) NOT NULL,
  `address` varchar(255) NOT NULL,
  `contact_no` text NOT NULL,
  `email_id` text NOT NULL,
  `timestamp` date NOT NULL,
  `status` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `token_id` (`token_id`),
  UNIQUE KEY `token_id_2` (`token_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=49 ;

All tables structure are same for car_showroom ,coaching , electronic .... like play_school table, in all tables ad_id='xyz' are same and the get id is also 'xyz'.now i want to fetch data from all table where ad_id='xyz'. 所有表的结构都与car_showroom,coaching,电子...一样,就像play_school表一样,在所有表中ad_id ='xyz'是相同的,并且get id也是'xyz'。现在我想从所有表中获取数据,其中ad_id = 'XYZ'。 This query is running without any error . 该查询正在运行,没有任何错误。 but didn't fetch the data. 但没有获取数据。

$email_id = $_GET['id'];

$result = $con->prepare("SELECT * FROM bike_showroom 
    JOIN car_showroom 
    JOIN coaching 
    JOIN college 
    JOIN electronic 
    JOIN furniture_showroom 
    JOIN hospital 
    JOIN job 
    JOIN mobile_shop 
    JOIN pets_shops 
    JOIN play_school 
    JOIN real_estate 
    JOIN services 
    JOIN shopping_store 
    JOIN stationary_shops 
    JOIN sweet_shop 
    WHERE bike_showroom.ad_id = '".$email_id.
      "' AND car_showroom.ad_id = '".$email_id.
      "' AND coaching.ad_id = '".$email_id.
      "' AND college.ad_id = '".$email_id.
      "' AND electronic.ad_id = '".$email_id.
      "' AND furniture_showroom.ad_id = '".$email_id.
      "' AND hospital.ad_id = '".$email_id.
      "' AND job.ad_id = '".$email_id.
      "' AND mobile_shop.ad_id = '".$email_id.
      "' AND pets_shops.ad_id = '".$email_id.
      "' AND play_school.ad_id = '".$email_id.
      "' AND real_estate.ad_id = '".$email_id.
      "' AND services.ad_id = '".$email_id.
      "' AND shopping_store.ad_id = '".$email_id.
      "' AND stationary_shops.ad_id = '".$email_id.
      "' AND  sweet_shop.ad_id = '".$email_id."' ");

$result->execute(); 
$row = $result->fetch();
for($i=0; $row = $result->fetch(); $i++){
    echo $row['title'];
}
1. If you want to JOIN tables , you need to JOIN them ON common fields.

2. The WHERE clause that you are using is not a JOIN parameter, but a FILTER.

3. You cannot possibly design a database where you JOIN one table  with 20 more tables. 

4. An example for you to understand. You have 2 tables, orders and orders_items

    table each order may contain several items that is why you need a second table called order_items. This table will have a column called order_id which will link to its parent table (ORDERS) and ON its id field.

You asked help to join the tables. I have given you the understanding you need. You are not showing us your tables, so we cannot possibly do the JOINS for you. This is the best you can get for your question. Lucky you 
have not been downvoted.

You need to use UNION clause. 您需要使用UNION子句。

$email_id = $_GET['id'];
$result = $con->prepare("SELECT * FROM (
    SELECT * FROM bike_showroom
    UNION ALL
    SELECT * FROM car_showroom 
    UNION ALL
    SELECT * FROM coaching 
    UNION ALL
    SELECT * FROM college 
    UNION ALL
    SELECT * FROM electronic 
    UNION ALL
    SELECT * FROM furniture_showroom 
    UNION ALL
    SELECT * FROM hospital 
    UNION ALL
    SELECT * FROM job 
    UNION ALL
    SELECT * FROM mobile_shop 
    UNION ALL
    SELECT * FROM pets_shops 
    UNION ALL
    SELECT * FROM real_estate 
    UNION ALL
    SELECT * FROM services 
    UNION ALL
    SELECT * FROM shopping_store 
    UNION ALL
    SELECT * FROM stationary_shops
    UNION ALL
    SELECT * FROM sweet_shop) as t WHERE ad_id = '".$email_id."'");

$result->execute(); 
 $row = $result->fetch();
for($i=0; $row = $result->fetch(); $i++){
    echo $row['title'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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