473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

MYSQL: Return any value on missing row.

56
Hi Guys,

I'm having problems returning a set of rows. What I have is two tables,
one to store properties and another storing the images (image paths) of the properties. Now a property isn't required to have an image. In that case I want to display an empty column or whatever (like null value). The thing is if a property doesn't have an image it's not listed in the images table. It's nonexistent.

I try to return all property adverts and each advert default image (only need the default one). I'm trying to achieve this in one SQL query below.

Expand|Select|Wrap|Line Numbers
  1. SELECT     pa.property_id_pk,                       
  2.          pa.property_price,                       
  3.          pa.property_refno,                       
  4.          pa.property_bedrooms,                    
  5.          pa.property_description,                 
  6.          pa.property_featured,                    
  7.          pa.property_sale_rent,                      
  8.          pa.advert_active,                         
  9.          pa.advert_start_date,                    
  10.          pa.advert_end_date,                                           
  11.          pim.image_url                            
  12. FROM    tb_property_advert as pa                     
  13. INNER JOIN tb_property_image as pim               
  14. ON pa.property_id_pk=pim.property_advert_id_fk 
  15. AND image_default = 1
Obviously this will only return the property adverts with a FK in the images table. What I need is to return the rest of property adverts (which have no image in the image table) and have it show as an empty column or null column.

Hope I explained myself well enough. Any ideas appreciated
Atli I think this is one for you :P

Thanks a lot
Luk
Jul 1 '08 #1
1 1669
pechar
56
I Solved it!!! 2 mins after I posted here sorry!
All I had to do is a LEFT JOIN instead of an INNER JOIN as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT     pa.property_id_pk,                       
  2.          pa.property_price,                       
  3.          pa.property_refno,                       
  4.          pa.property_bedrooms,                    
  5.          pa.property_description,                 
  6.          pa.property_featured,                    
  7.          pa.property_sale_rent,                      
  8.          pa.advert_active,                         
  9.          pa.advert_start_date,                    
  10.          pa.advert_end_date,                                           
  11.          pim.image_url                            
  12. FROM    tb_property_advert as pa                     
  13. LEFT JOIN tb_property_image as pim               
  14. ON pa.property_id_pk=pim.property_advert_id_fk 
  15. AND image_default = 1
Please correct me if I'm wrong but it seems to be giving me the results I want!

Thanks anyway
Luk
Jul 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
0
by: mdh | last post by:
I am trying to learn the basics of MVC applications using a Tomcat infrastructure. I'm starting by building a simple application with: * a login.jsp page for a basic login form with a action...
5
by: Tom Martin | last post by:
I'm a Java Web developer NEWBIE that has inherited a website that fails every 2 hours due to poor connection pooling between Tomcat 4.0.6 and mySQL. In efforts to resolve this problem, I've...
0
by: dohnut | last post by:
Here's one for some bored problem solver :) I ran across this earlier today and fixed it, but don't exactly know why. (that usually only happens in C :) I'm using Perl version 5.8.0 btw. ...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
4
by: Ewok | last post by:
let me just say. it's not by choice but im dealing with a .net web app (top down approach with VB and a MySQL database) sigh..... Anyhow, I've just about got all the kinks worked out but I am...
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.