Solved

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

Posted on 2011-02-25
4
331 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
  • 2
  • 2
4 Comments
 
LVL 24

Expert Comment

by:mankowitz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect, thanks for clearning up in my mind how to do a join of more than one table
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now