Solved

Best way to query database.  One-to-one query

Posted on 2011-02-25
4
338 Views
Last Modified: 2012-05-11
Hello All,

I have the following (working) query for my website that I have included in the code section.

What I want to achieve from the query:
Take all rows from table 'takers' with completed=1 and quiz_ID=30
Use the user_id on each selected row from 'takers' and select the SINGLE ROW to get matching username from 'users' table
Use the personality_result (which is an ID) to select the matching SINGLE ROW from the 'personalities' table

I stumbled across the syntax that I have used, where you just comma seperate the table names (FROM takers, users, personalities) and then match one row with another (WHERE takers.user_id = users.user_id).

Should I be using a join, or is this the right way to do it if it's just selecting a row in a one-to-one fashion?

But for some reason this doesn't feel right... am I missing something?

Thanks again,

Luke
SELECT taker_ID, takers.user_id, taker_name, start_time, percentage_score, personality_result, users.username, personality_name
FROM takers, users, personalities
WHERE takers.user_id = users.user_id
AND takers.personality_result = personalities.personality_ID
AND completed='1'
AND takers.quiz_ID='30'

Open in new window

0
Comment
Question by:stellaartois
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 34980222
when you do a select with multiple table names and specify the correlations in a WHERE clause, you are doing an implicit inner join.

To answer your question "should I use a join?", you ARE using a join. You don't need to use the JOIN keyword unless you want a different kind of join (e.g. LEFT OUTER JOIN)

Also, if you want a single line, you may need to specify "LIMIT 1"
0
 
LVL 2

Author Comment

by:stellaartois
ID: 34980417
Thanks Manowitz for the response.

From what I have read, it seems that I want to use the explicit inner join notation to ensure that only those with matching results exist.

I am struggling to put this query in to the explicit syntax because there are 3 tables involved.

Do you happen to know how I would rewrite this query to be in the explicit notation?

Thanks again,

Luke
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 250 total points
ID: 34980804
SELECT taker_ID, takers.user_id, taker_name, start_time, percentage_score, personality_result, users.username, personality_name
FROM takers,
LEFT INNER JOIN users ON (takers.user_id = users.user_id)
LEFT INNER JOIN personalities ON (takers.personality_result = personalities.personality_ID)
WHERE
completed='1'
AND takers.quiz_ID='30'
0
 
LVL 2

Author Closing Comment

by:stellaartois
ID: 34987584
Perfect, thanks for clearning up in my mind how to do a join of more than one table
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question