Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-25
4
Medium Priority
?
339 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 1000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 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