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.

Recommended Answers

All 4 Replies

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
Member Avatar for diafol

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.