I am not good in sql so please don't scold me for such basic question. Currently I am doing a lot of homework on other technologies so I will improve sql as and when I get time to study it. Anyway let come to the point.
My question is for below query. I am trying to fetch data from three tables and it works fine as long as all conditions are satisfied but the problem is that for a posting it may happen that images are not present i.e not record for a posting in image table then post.post_id = img.post_id and img.sequence='1'
condition fails and no row is returned but I want row to be returned even if no record present in images for the posting. In such case return null for image path column.
SELECT
img.path as path, pav.value_text as beds, post.post_id as postID
FROM
postings post, post_attributes_values pav, images img
where
post.post_id = pav.post_id and post.status !='expired' and pav.attr_id='33' and post.post_id = img.post_id and img.sequence='1' and post.post_id=49
If all condition satisfies then O/P comes like
path beds postId
-----------------------------------------------
saome path value 2 49
but if post.post_id = img.post_id and img.sequence='1'
condition not satisfied then O/P should be like:
path beds postId
----------------------------------------
null 2 49
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `images` ADD INDEX `images_idx_sequence_post_id` (`sequence`,`post_id`);
ALTER TABLE `post_attributes_values` ADD INDEX `post_values_idx_attr_id_post_id` (`attr_id`,`post_id`);
ALTER TABLE `postings` ADD INDEX `postings_idx_post_id_status` (`post_id`,`status`);
SELECT
img.path AS path,
pav.value_text AS beds,
post.post_id AS postID
FROM
postings post,
post_attributes_values pav,
images img
WHERE
post.post_id = pav.post_id
AND post.status != 'expired'
AND pav.attr_id = '33'
AND post.post_id = img.post_id
AND img.sequence = '1'
AND post.post_id = 49