Using JOIN when second record might not exist [duplicate] - mysql

This question already has answers here:
Where clause for only one table in a left join
(4 answers)
Closed 5 years ago.
I'm dealing with two tables - users with about 20 million rows, and data with about 2 billion rows.
I need to select all users rows where active=1, and then join that with their corresponding data row where users.username=data.username AND data.date='2017-11-30'.
The catch is that many of these users won't have a data row where date='2017-11-30', but I still need their record to be returned, just without any info for that date.
What would be the most resource-efficient way to accomplish this? I got a start with this, but it doesn't look quite right:
SELECT users.username FROM users
INNER JOIN data ON data.username = users.username
WHERE
users.active = 1 AND data.date = ‘2017-11-30’

It sounds like you can just do a left join:
SELECT *
FROM users LEFT OUTER JOIN data
ON data.username = users.username AND data.date = '2017-11-30'
WHERE
users.active = 1

Related

How to remove duplicate results? [duplicate]

This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 3 years ago.
I'm coding a message system using mysql.
Everything works fine when I list users whom I'm conversing with, until I want to add date of the last or the start of conversation.
When I add a.date I get duplicate results when the date isnt the same.
Here is my sqlfiddle
Since, you were pulling only user_id then in both cases (send/recieve) it was giving you distinct record. But now with date it is no more distinct. you need to do something like:
SELECT temp.id_user, MAX(temp.date) as date
FROM
(
SELECT users.id_user,
a.date
FROM users
LEFT JOIN message AS a
ON users.id_user = a.id_user_recipient
LEFT JOIN message AS b
ON a.id_user_recipient = b.id_user_sender
WHERE a.id_user_sender = 1
UNION DISTINCT
SELECT users.id_user,
a.date
FROM users
LEFT JOIN message AS a
ON users.id_user = a.id_user_sender
LEFT JOIN message AS b
ON a.id_user_sender = b.id_user_recipient
WHERE a.id_user_recipient = 1
) as temp
GROUP BY temp.id_user;
Grabbing max(date) will ensure to return only one record as with group by

Joining a table in mysqli and then fetch the matched result [duplicate]

This question already has answers here:
Select all records don't meet certain conditions in a joined table
(2 answers)
Closed 5 years ago.
i am having two tables in my DB like members and payments . Table Members has the name and id of users and payment table has the id,amount and session of payments like if user 1 has paid 1500 for a session 3 then table payment has the following details. mid 1, session 3 and amount 1500.
Now i want to fetch the names of all the users which have not aid for session 1
i am using the following query but it is not working
SELECT NAME
,id
FROM member m
,payment p
WHERE (
p.session = '3'
AND m.id != p.mid
)
This is not giving me the required result please help me .
What about something like this:
SELECT NAME
,id
FROM member m
inner JOIN payment paid
ON m.id = paid.mid
AND paid.sessionid = '1'
LEFT JOIN payment p
ON m.id = p.mid
AND p.sessionid = '3'
WHERE p.id IS NULL
When you want to get data from more than 2 tables , you have to use join and if you don't want to use join as you are doing, then make sure you have exact relation between that tables, e.g a.id = b.membe_id etc..
And in your case, i think the relation is not right, make sure you have something common in two tables.
Thanku.

MySQL join a table to another table only if [duplicate]

This question already has answers here:
conditional join in mysql
(4 answers)
Closed 6 years ago.
I have a little complicated query here. I have to select columns by names cant just user 'orders.*. because i have multiple columns from different tables with the same name.
What I'm trying to do is to select specific fields from orders,users,payment_methods_translation and join bank_accounts_translation only if the orders.payment_method_id = '3'
SELECT
orders.id as orderid,
orders.final_total,
orders.user_id,
orders.auto_cancel,
users.id as userid,
users.first_name,
payment_methods_translation.payment_method_id,
payment_methods_translation.name
FROM
orders,users,
payment_methods_translation
WHERE
orders.id='$id' AND
orders.user_id = users.id AND
orders.payment_method_id = payment_methods_translation.payment_method_id AND
orders.auto_cancel='1'
JOIN
bank_accounts_translation ON (orders.payment_method_id='3'
AND orders.bank_id = bank_accounts_translation.bank_account_id)
But I get a mysql error. So how can I select all the fields from bank_accounts_translation only if orders.payment_method_id = '3' and orders.bank_id = bank_accounts_translation.bank_id
Left join should do it... I
Refactored the code to use the current ANSI standards, you appear to be mixing them.
used table aliases to improve readability.
LEFT JOIN says return all records from prior listed tables and only those that match the on criteria of the table being joined to.
SELECT O.id as orderid
,O.final_total
,O.user_id
,O.auto_cancel
,u.id as userid
,u.first_name
,PMT.payment_method_id
,PMT.name
FROM orders O
INNER JOIN users U
ON O.user_id = U.id
INNER JOIN payment_methods_translation PMT
ON O.payment_method_id = PMT.payment_method_id
LEFT JOIN bank_accounts_translation BAT
ON O.payment_method_id='3'
and O.bank_id = BAT.bank_account_id
WHERE O.id='$id'
and O.auto_cancel='1'

Table inner join or selecting mutiple tables [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
INNER JOIN ON vs WHERE clause
INNER JOIN:
SELECT *
FROM PETS
INNER JOIN OWNER ON PETS.OWNER_ID = 1
AND OWNER.ID = 1
MULTIPLE TABLE SELECT:
SELECT *
FROM PETS, OWNER
WHERE PETS.OWNER_ID = 1
AND OWNER.ID = 1
Is one better than the other?
Faster than the other?
They seem to produce exactly the same results.
Is there any difference at all?
I am trying to learn the best methods. In using the join, I noticed that the exact same thing can be achieved with the multiple table call
read this answer about the difference between a cross join and an inner join Performance of inner join compared to cross join

SQL: Query Entire Database

I am a complete beginner in SQL. I am using a program that queries a database, and then processes the results. The default query is:
SELECT *
FROM data,
questions,
users
where users.U_Id = data.Subj_Id
and data.Subj_Id between 1 and 10
and data.Q_Id = questions.Q_Id
and questions.Q_Id between 1 and 10
order by Subj_Id;
I'd like it to query every Subj_Id and every Q_Id. I do not know how many there are of either, and different subjects have different numbers of questions. How should I alter the above query?
You can rewrite the above query like this.
select *
from
data
inner join
users on users.U_Id = data.Subj_Id
inner join
questions on data.Q_Id = questions.Q_Id
where data.Subj_Id between 1 and 10
and questions.Q_Id between 1 and 10
order by Subj_Id;
This makes it clearer by separating the joins between tables from the filters on the data.
So to query the entire database, you just remove the where clause from the above...
select *
from
data
inner join
users on users.U_Id = data.Subj_Id
inner join
questions on data.Q_Id = questions.Q_Id
order by Subj_Id;