0

Hi I have these two tables:Customers and Bookings;Customers has these fields CustomerId,firstname,lastname etc.Table bookings has bookingId, customerId,datebooking,startdate,enddate etc.I want to retrieve customerId,first and last name from Customers and datebooking,start and enddate from bookings, I understand I have to use a LEFT JOIN.I tried everything but it does not work, coud someone give me the right code for this? I am running MySQL 5.4.

3
Contributors
4
Replies
14
Views
4 Years
Discussion Span
Last Post by Fiorentino01^
0

You need to read left join properly to understand its syntax.Check out its syntax at http://dev.mysql.com/doc/refman/5.0/en/join.html
You can even go to some example here to know its usage http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php

By the way following will be the way to solve.Please go through the above code to learn it rather than just copying this and using.

SELECT  Customers.customerId,Customers.firstname,Customers.lastname,bookings.datebooking,bookings.startdate , bookings.enddate
FROM Customers 
LEFT JOIN bookings
ON Customers.customerId=bookings.customerId
0

Are you sure you want a LEFT JOIN? An INNER JOIN sounds more likely for a bookings list. The left join will give you a list of all the customers, regardless of whether they've made bookings.

0

Thank you very much I have seen the difference looking at your code and what I found in some tutorials.I am not just going to copy and paste it, actually I am spending a lot of time trying to figure out how to correctly code what I am trying to accomplish, is not easy but I am slowly getting there, thanks again for your help.

0

No I am not sure that I need a LEFT JOIN instead of an INNER JOIN, frankly I have not figured out exactly
the difference between the various JOIN examples, so I keep studying.I'll try the INNER JOIN and see what happens.The problem is that the code of the examples in the tutorials often is a few years old and not compatible I think with my version of MySQL.Thank you

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.