Answer the question
In order to leave comments, you need to log in
Selecting data from three MySQL tables with a many-to-one condition?
Hello dear (and not so) geeks.
For a long time I was tormented by the question regarding the following situation:
There are three fictitious tables:
1. The declaration table
CREATE TABLE `ads` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NULL DEFAULT NULL,
`cat_id` INT(11) NULL DEFAULT '0',
`city_id` INT(11) NULL DEFAULT NULL,
`phone` VARCHAR(100) NOT NULL,
`title` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`),
INDEX `user_id` (`user_id`),
INDEX `cat_id` (`cat_id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
AUTO_INCREMENT=0;
CREATE TABLE `ads_meta` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ads_id` INT(11) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`integer` INT(20) NOT NULL,
`varchar` VARCHAR(100) NOT NULL,
`text` TEXT NOT NULL,
`longtext` LONGTEXT NOT NULL,
PRIMARY KEY (`id`),
INDEX `ads_id` (`ads_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=0;
CREATE TABLE `ads_images` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` INT(11) NOT NULL DEFAULT '0',
`ads_id` INT(11) NOT NULL,
`path` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`),
INDEX `ads_id` (`ads_id`),
INDEX `user_id` (`user_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=0;
Answer the question
In order to leave comments, you need to log in
I don't catch up a bit.
With one request:
select ads.id, ads_meta.text, ads_images.path ...
from ads, ads_images, ads_meta
where ads_images.ads_id=ads.id and ads_meta.ads_id=ads.id
order by ads.id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question