Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9180711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:07:12+00:00 2026-06-17T18:07:12+00:00

I have a MySQL database with three tables: sample , method , compound .

  • 0

I have a MySQL database with three tables: sample, method, compound.

sample has the following columns: id(PK)(int), date(date), compound_id(int), location(varchar), method(int), value(float)

method has the following columns: id(PK)(int), label(varchar)

And compound has: id(PK)(int), name(varchar), unit(varchar)

I am trying to generate a SQL command that only pulls in the unique row for the following criteria:

  • Date (sample.date)
  • Compound Name (compound.name)
  • Location (sample.location)
  • Method (sample.method)

However, I want to substitute in the labels for some of the sample columns instead of the numbers:

  • sample.compound_id is matched to compound.id which has a corresponding compound.name and compound.unit

The first SQL command I tried to query was:

SELECT sample.id, sample.date, compound.name, sample.location, method.label, sample.value, compound.unit
FROM sample, compound, method 
WHERE sample.date = "2011-11-03"
AND compound.name = "Zinc (Dissolved)"
AND sample.location = "13.0"
AND method.id = 1;

The output from the above command:

id      date        name            location    label       value   unit
1       2011-11-03      Zinc (Dissolved)    13.0        (1) Indivi...   378.261     μg/L
5       2011-11-03      Zinc (Dissolved)    13.0        (1) Indivi...   197.917     μg/L
9       2011-11-03      Zinc (Dissolved)    13.0        (1) Indivi...   92.4051     μg/L

But when I look at sample and compare sample.id to what was returned:

id      date    compound_id     location     method     value   
1       2011-11-03  13          13.0         1          378.261
5       2011-11-03  14          13.0         1          197.917
9       2011-11-03  47          13.0         1          92.4051

Where compound.id 47 corresponds to compound.id 47 and compound.name “Zinc (Dissolved)”. Compound IDs #13 and #14 are “Copper (Dissolved)” and “Copper (Total)”, respectively.

So it seems to be returning rows that meet the criteria for sample.date and sample.location without regard to compound.name. Given the above criteria, I know that my database should only return one row, but instead I get some sample.id rows that have a completely different sample.compound_id than the matching compound.name that I specified.

I would like to end up with the columns that are SELECTed in the first line to end up in the same order as I wrote them. This code is for a little database viewer/reporter program I’m writing in Python/Tkinter and relies on the columns being uniform. The code that I use to initialize the data for the program works as I expect:

SELECT sample.id, sample.date, compound.name, sample.location, method.label, sample.value, compound.unit
FROM sample, compound, method 
WHERE sample.compound_id = compound.id
AND sample.method = method.id;

Which puts out each unique line in sample with the substitutions for sample.compound_id to compound.name and sample.method to method.label and adds in the compound.unit at the end.

Question #1: How do I need to restructure my query so that it only returns the row that meets that specific criteria?

Question #2: Eventually I’m going to need to specify multiple sample.locations at one time. Is that as simple as adding an OR statement for each individual location that I need?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:07:13+00:00Added an answer on June 17, 2026 at 6:07 pm
    SELECT sample.id, sample.date, compound.name, sample.location, method.label, sample.value, compound.unit
    FROM sample 
    INNER JOIN compound ON compound.id = sample.compound_id 
    INNER JOIN method ON method.id = sample.method
    WHERE sample.date = '2011-11-03'
    AND compound.name = 'Zinc (Dissolved)'
    AND sample.location = "13.0"
    AND method.id = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three mysql table from same database Db1. Three tables have following columns.
I have the following three tables in a MySQL database in order to give
I have three tables in a Mysql database - countries, cities and hotels. Their
I have three or four tables in a MySQL database associated with an upcoming
I'm making a cocktail database, and right now I have three tables: drinks has
I have a MySQL database which has 10 tables in it. The database structure
I have the following task: there are a simple table from mysql database: <html>
I have a MySQL database set-up in a hierarchy style. There are 4 tables
I have MySQL database, where I store the following BLOB (which contains JSON object)
I have a MySQL database holding hierarchical data using the Closure Table method. A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.