Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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