我想加入所有表,在所有表中,所有列名称都是相同的。我的查询在下面。请帮助我获取数据。我的表结构是:
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,教练,电子....像play_school桌子一样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'];
}
您需要使用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'];
}
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.