473,398 Members | 2,389 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,398 software developers and data experts.

Left Join Query MySQL Database

Man have I hit a pitfall on this one--I need help figuring out how to join three tables to display results in this format:

Obi One
downloaded adobe.pdf
downloaded apples.pdf
downloaded mymusic.mp3
Luke Skywalker - 555-5455
downloaded sample.pdf
downloaded apples.pdf
downloaded myband.mp3
So as you can see I have a users table which stores first and last name, and phone number, I've got another table where I store the available downloads identifiable by a unique ID and lastly I have a third table where I store the id of the user and the id of the item thy downloaded. But for the life of me I just don't understand how to join the tables and display each person in that particular format with the title and then the information they've downloaded.

any help in the right direction will be forever appreciated!!

Thanks a bunch!

Atlante
Oct 8 '11 #1
7 2155
Rabbit
12,516 Expert Mod 8TB
You join the third table to users table on the user id and then join the to the downloads table by the download id. No need for a left join.
Oct 9 '11 #2
Thanks Rabbit--do you happen to have a tutorial where I can see and analyze a similar code? Thanks again!
Oct 9 '11 #3
Rabbit
12,516 Expert Mod 8TB
Something similar would be this
Expand|Select|Wrap|Line Numbers
  1. SELECT a.FullName, b.DownloadName
  2. FROM tblUsers AS a
  3.    INNER JOIN tblUserDownloads AS c
  4.    ON a.UserID = c.UserID
  5.  
  6.    INNER JOIN tblDownloads AS b
  7.    ON b.DownloadID = c.DownloadID
Oct 9 '11 #4
Okay, I've got this piece of php code here and finally got it to work! The only problem is this a single user can download multiple files, and when I execute the code below, it returns a list of the name of the user and what files he downloaded as expected, but I just want it to return the user's information once and the files he downloaded below it like this:

1. Full Name | 555-555-5555 | sampleemail@domainname.com
  • Archives.pdf
  • Excel-Sheet.xls
  • FileName.pdf

2. Jane Doe | 555-555-5555 | sampleemail@domainname.com
  • FileName.pdf

Here's the code any help is truly appreciated!!!

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $sql = "SELECT \n"
  3.     . " usrid,\n"
  4.     . " name,\n"
  5.     . " phone,\n"
  6.     . " email,\n"
  7.     . " downloadid,\n"
  8.     . " filename\n"
  9.     . "FROM\n"
  10.     . " downloaded d\n"
  11.     . "INNER JOIN\n"
  12.     . " download_manager dm\n"
  13.     . "ON\n"
  14.     . " (d.downloadid = dm.id)\n"
  15.     . "INNER JOIN\n"
  16.     . " tz_members tz\n"
  17.     . "ON\n"
  18.     . " (d.usrid = tz.id) ORDER BY `tz`.`name` ASC LIMIT 0, 30 ";
  19.  
  20.     $result = mysql_query($sql) or die(mysql_error());
  21.  
  22.     while($row = mysql_fetch_array($result)){
  23.         echo "<div id=\"file-manager\"><p style=\"color: blue;\">" . $row['usrid'] . " - ".$row['name'] . " - " .$row['phone']  . " - " .$row['email'] . " - " .$row['filename']; ?>
  24. <?php echo "</p></div>";} ?>
Oct 11 '11 #5
Rabbit
12,516 Expert Mod 8TB
Just keep track of the name and print it only when it changes.
Oct 11 '11 #6
Thanks again Rabbit, but unfortunately that's exactly what I don't know how to do, do you have a piece of code or a reference? Thanks a bunch!
Oct 11 '11 #7
Rabbit
12,516 Expert Mod 8TB
Basically, in pseudocode, it would be
Expand|Select|Wrap|Line Numbers
  1. ClientName = ""
  2.  
  3. while(row = read a row)
  4.    if row('ClientName') != ClientName
  5.       print row('ClientName')
  6.       print row('Download')[/
  7.       ClientName = row('ClientName')
  8.    else
  9.       print row('Download')
Oct 11 '11 #8

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

Similar topics

6
by: Dave | last post by:
To build a grid, all the distinct rows from T1 are required, and only those from T2 which fall btn 2003-09-11 and 2003-09-18. In the following example: A is included because it is within the date...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
0
by: Leif Wessman | last post by:
Hi! Please have a look at the following query: SELECT BOOKS.ISBN, BOOKS.TITLE, IMAGES.WEIGHT, IMAGES.WIDTH FROM BOOKS LEFT JOIN IMAGES ON BOOKS.ISBN = IMAGES.ID WHERE MATCH(BOOKS.TITLE)...
1
by: David Boone | last post by:
Please excuse the newbie questions, but I'm fairly new to Access though familiar with databases in general (experience with MySQL and PostgreSQL). The basic idea is that there is table of...
0
by: mlarson | last post by:
I have a program that worked fine then they needed to be able to also see the empty cells (inmate cells) on a housing unit when they ran the query. So what I had to do was take two tables and...
2
by: David F | last post by:
Hello, I have what seems like a simple left join query, but Access returns results like it's an inner join. I've tried numerous combinations without success. I have a table (ProjectList)...
1
by: sesmap | last post by:
I am not certain if I set this up correctly - all help would be appreciated: SELECT .ACCOUNTID, .PUBCODE, .NAME, .PIATYPE, .FOD, .SUBSLNGTH, .RATECODE, .CURRENTBAL, .EOGDATE, .TELEPHONE,...
2
by: imnewtoaccess | last post by:
Hello everybody, I have two tables : ac_online_scanned_tkts_1 and TK_IMPORT_TICKET1. Both have external barcodes of tickets, but not all of them in ac_online_scanned_tkts_1 table exist in...
2
by: knkk | last post by:
Please see this: WARNING Error while executing query: select event_type, EventId, VenueId, Price, Schedule, ev.startDate, ev.endDate, events.name eventname, movies.name moviename from...
2
Coldfire
by: Coldfire | last post by:
Two tables customer (id,name) = { '1','ABC'; '2','DEF'; '3','GHI' } images (id,customer_id,subscription_id,imageURL) = { '1','1','123','abc123.jpg';
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.