簡體   English   中英

在php mysql中加入表

[英]Table Join in php mysql

我想加入所有表,在所有表中所有列的名稱都相同。 我的查詢如下。 請幫助我獲取數據。 我的表結構是:

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 ;

所有表的結構都與car_showroom,coaching,電子...一樣,就像play_school表一樣,在所有表中ad_id ='xyz'是相同的,並且get id也是'xyz'。現在我想從所有表中獲取數據,其中ad_id = 'XYZ'。 該查詢正在運行,沒有任何錯誤。 但沒有獲取數據。

$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.

您需要使用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