Solved

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

Posted on 2011-02-25
4
337 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a MySQL table as easily as possible 6 71
How do uses indexes to maximize MySQL Searches 14 78
CLI command keep running after close 7 73
phpmyadmin memory error 55 96
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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