Understanding SQL Queries (Subqueries, where Clauses

Asked 1 years ago, Updated 1 years ago, 39 views

There are three tables in the DB on SQL: user, audit, and changelog.
The columns are as follows:

[user]
id,email,date

[audit]
user_id, difference_id, date

[changelog]
difference_id, log, year, month, date

※ user.id and audit.user_id are common data

I would like to index user.email from changelog.difference_id, but I would definitely like the where clause to include where date_format (CURRENT_TIMESTAMP, '%Y%m%d') and return the result by date.
I'd like to consolidate the queries into one, but I don't know how to describe the subqueries. (Specifically, I'm confused by the situation where there are multiple conditions and I'm using subqueries.)

In short, I would like to summarize the following two queries and submit the one with the date today (dynamic).
select user_id
from audit
where changelog.difference_id='{{9fjei347fyw83926r}}'
Return →user_id=1007

select email
from user inner join audit on id=user_id
where user_id='{{1007}}'

I'm a beginner, and I don't know what it means, but I'd appreciate it if you could teach me as gently as possible.
I look forward to your kind cooperation.

sql

2022-09-30 19:53

1 Answers

与えられたI want to find records in the changelog table from the given difference_id and see the email of the records in the user table associated with them. および and month tables can also be narrowed down to No subqueries are required.

select
    changelog.difference_id
    , user.email
from
    changelog
    inner join audit on changelog.difference_id=audit.difference_id
    inner join user on audit.user_id=user.id
where
    changelog.difference_id = Enter the ID you want to check here
    and changelog.year="2022" and changelog.month="06" and changelog.date="17"

I would like to make sure that the where clause includes "where date_format(CURRENT_TIMESTAMP, '%Y%m%d')", and return the result by date.

I'm not sure if this part of the questionnaire is correct.If that's not the case, it's easy to see if there's an example of what you want based on sample data and sample conditions.

In particular, if you know the difference_id, you can find the records in the changerog table, so I didn't really understand the intention of narrowing it down by date.


2022-09-30 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.