Mysql join query - mysql

I have table users and another table premium_users in which I hold the userid and the date when he bought premium membership.
How can I use mysql join , so that in a single query I can select all the columns from the table users and also know for each premium user the date he joined on.
USERS:
ID USERNAME
1 JOHN
2 BILL
3 JOE
4 KENNY
PREMIUM USERS:
ID USERID DATE
1 2 20/05/2010
2 4 21/06/2011
And the final table (the one that will be returned my the query) should look like this:
ID USERNAME DATE
1 JOHN
2 BILL 20/05/2010
3 JOE
4 KENNY 21/06/2011
Is it ok for some rows to have the DATE value empty?
How can I check if that value is empty? $row['date']=='' ?
EDIT:
This was only an example, but the users table has much more columns, how can I select all from users and only date from premium_users without writing all the columns?

select u.*, pu.DATE
from USERS u LEFT OUTER JOIN PREMIUM_USERS pu on
u.ID = pu.USERID
You can check if a row is empty with:
if (!$row['DATE'])
{
...
}

select USERS.ID, USERS.USERNAME, PREMIUM_USERS.DATE
from USERS
join PREMIUM_USERS on USERS.ID = PREMIUM_USERS.ID
order by USERS.ID

This is mssql syntax, but it should be pretty similar...
select *
from users u
left join premiumUsers p
on u.id = p.id
order by u.id asc

SELECT A.*, B.DATE
FROM USERS A
LEFT JOIN PREMIUIM_USERS B on A.ID=B.USERID
EDITED

It might be easier to have it all in one table. You can have nullable fields for isPremium(t/f) and premiumDate. you actually dont even need the isPremium field. just premiumDate if its null they are not premium and if it has value they are premium user and you have the date they joined.

Related

Query only return ids where does't have a relationship with where

Doing a query on some data but I can't workout how to do this efficiently-- querying users and joining with a table of dates-
I want the users who have do NOT have a record in the second table with a matching date but they have multiple records so if I just did select * from users join dates on users.user_id=dates.user_id where dates.date != '8/3/2020';, shevy and rob would still be returned because row 4 still matches for shevy.
users dates
------- -------
id name id user_id date
1 shevy 1 2 8/1/2020
2 rob 2 1 8/3/2020
3 2 8/10/2020
4 1 8/17/2020
Personally, I would use NOT EXISTS...
SELECT U.ID
,U.NAME
FROM USERS U
WHERE NOT EXISTS (
SELECT 1
FROM DATES D
WHERE D.USER_ID = U.ID
AND D.DATE = '8/3/2020'
)
;
As an aside, this seems like a basic ANSI SQL question and is not really a Snowflake question.
I think you want:
select u.*
from users u left join
dates d
on u.user_id = d.user_id and d.date = '8/3/2020' -- should be 2020-08-03
where d.date is null;
This gets you users that do not have the specified date.

How to JOIN SELECT from multiple tables, where the SELECTS is based on different conditions?

I have three tables. One with notes Notes, one with users Users, and one a relational table between users and notes NotesUsers.
Users
user_id first_name last_name
1 John Smith
2 Jane Doe
Notes
note_id note_name owner_id
1 Math 1
2 Science 1
3 English 2
NoteUsers
user_id note_id
1 1
2 1
2 2
2 3
Hopefully, from the select statement you can tell what I'm trying to do. I am trying to select the notes that user_id = 2 has access to but doesn't necessarily own, but also along with this I'm trying to get the first and last name of the owner.
SELECT Notes.notes_id, note_name
FROM Notes, NotesUsers
WHERE NotesUsers.note_id = Notes.note_id AND NotesUsers.user_id = 2
JOIN SELECT first_name, last_name FROM Users, Notes WHERE Notes.owner_id = Users.user_id
My problem is that because the WHERE clause for first_name, and last_name versus that for notes are different, I don't know how to query the data. I understand that this is not how a JOIN works and
I don't necessarily want to use a JOIN, but I'm not sure how to structure the statement, so I left it in there so that you can understand what I'm trying to do.
You can join Notes with NoteUsers to check for access and with Users to add the user's details to the result:
SELECT n.noted_id, n.note_name, u.first_name, u.last_name
FROM Notes n
JOIN NoteUsers nu ON n.noted_id = nu.note_id AND nu.user_id = 2
JOIN Users u ON n.owner_id = u.user_id
you need here to use a query inside the main query. MySQL will return first all the note_id that the user with user_id = 2 has access to from NoteUser, then well build the outer query to return the first_name and the last_name of the owner.
SELECT u.first_name, u.last_name, n.note_name, n.note_id
FROM Notes AS n
LEFT JOIN Users AS u ON u.user_id = n.owner_id
WHERE n.note_id IN
(SELECT nu.note_id FROM NoteUser WHERE nu.user_id = 2)

How to get data from database with condition on another table where something exist or not

I have a table for users like this
id | name | password | email
1 saeid ***** asd#asd.com
I have another table called appointments
id | created_by | due_date | notification_send
1 1 ***** 0
I want to get all users from users table where they have at least created one appointment in the appointments table (denoted by created_by field in the appointments table).
I have tried the code below but it fails:
SELECT * FROM users LEFT JOIN appointments a ON persons.id = a.created_by
But obviously it does not work.
One way is to use the exists predicate:
SELECT * FROM users u
WHERE EXISTS (SELECT 1 FROM appointments a WHERE a.created_by = u.id)
Alternatively you could use an inner join, but the exists query corresponds better to your question in my opinion (that is if you only need data from the users table).
The left join says to get all rows from users regardless if they have matching rows in appointments which is not what you want.
You are searching for a match between the table and so I would suggest doing a INNER JOIN rather like below
SELECT * FROM users u
JOIN appointments a ON u.id = a.created_by
Also check your ON clause once I think either this is a typo or a big mistake. You are selecting from users table then why persons.id??
ON persons.id = a.created_by
Try something like this:
http://sqlfiddle.com/#!9/5eba3/2
select * from users c where (select count(*) from appointments where created_by = c.id) > 0;

SQL find records based on two columns

Given a users table with primary key of id, I have a foreign table called friends which only has two fields (userid1, userid2). This allows us to create any kind of relationship between different users (one to many, one to one, etc). A user can appear in either column and both columns are equal. IOW, a single entry per relationship.
How can I pull all of the friends that a given user id has. Say Jonny, has 3 friends and his user id is 16... should my sql query look like this?
SELECT *
FROM db.users
JOIN db.friends
ON db.users.id = db.friends.userid1
AND db.users.id = 16
Hopefully, this is clear. Also, if possible, can I exclude Jonny from the result set?
This query, as listed gies me the following:
id name uuid birthday userid1 userid2
16 jonny ABCDEFGHIJKLMNOP 1967-04-27 01:00:00 1 2
16 jonny ABCDEFGHIJKLMNOP 1967-04-27 01:00:00 1 3
This is pretty close, except I want his friends, not jonny
Thanks guys, so I got it to work thanks to you. Here is the final working query.
SELECT *
FROM db.users
WHERE db.users.id IN
(
SELECT db.friends.userid2 as id FROM db.friends WHERE db.friends.userid1 = 16
union
SELECT db.friends.userid1 as id FROM db.friends WHERE db.friends.userid2 = 16
)
which gives me:
id name uuid birthday
2 robin ABCDEFGHIJKLMNOP 1967-04-27 01:00:00
3 gary ABCDEFGHIJKLMNOP 1967-04-27 01:00:00
You could do a sub query like:
SELECT *
FROM users
WHERE id IN
(
SELECT userid2 as id FROM db.friends WHERE userid1 = 16
)
Add the condition for the user.id to your where clause at the end:
Select * From users
INNER JOIN friends on
users.id = friends.userid1
Where users.id = 16
Also, I would use an Inner Join which will return all records from users only where there is a match in friends
You should filter on the friends table, not the users table.
SELECT friends.*
FROM friends
INNER JOIN users
ON friends.userid2 = users.id
WHERE friends.userid1 = 16
If you just need the friend ID's then there is not reason to join at all
SELECT userid2
FROM friends
WHERE userid1 = 16
You need a list of friends ids:
SELECT U
FROM DB.USERS U
WHERE U.ID IN ( SELECT F.USERID2 FROM DB.FRIENDS F WHERE F.USERID1 = 16)

basic join on mysql

I am having a hard time understanding joins on mySQL, and I cannot find any similar example to work with.
Suppose I have two tables: users and users_info.
in users I have id, email and password fields while, in users_info I have all their information, like name, surname, street, etc.
so, if I am getting a user like this:
SELECT * FROM users WHERE id = 43
and their information like this:
SELECT * FROM users_info WHERE id = 43
I will basically get 2 results, and 2 tables.
I understand now that I need to use join so that they are all together, but I just can't figure out out.
Any help?
It seems like both tables users and user_info are related with each others by the column id therefore you need to join them using this column like this:
SELECT
u.id,
u.email,
u.password,
i.name,
i.surname,
i.street
FROM users AS u
INNER JOIN user_info AS i ON u.id = i.id;
This will only select the fields id, email, ... etc. However, if you want to select all the columns from both the tables use SELECT *:
SELECT *
FROM users AS u
INNER JOIN user_info AS i ON u.id = i.id;
If you want to input the id and get all of these data for a specific user, add a WHERE clause at the end of the query:
SELECT *
FROM users AS u
INNER JOIN user_info AS i ON u.id = i.id
WHERE u.id = 43;
For more information about JOIN kindly see the following:
Join (SQL)From Wikipedia.
Visual Representation of SQL Joins.
Another Visual Explanation of SQL Joins.
Here's an example
SELECT * FROM users u
INNER JOIN users_info i
ON u.id=i.id
this means, you are joining users table and users_info table
for example
users
id name
---- -------
1 abc
2 xyz
users_info
id email
--- ------
1 abc#aaa.com
2 xyz#aaa.com
the query will return
id name email
--- ----- -------
1 abc abc#aaa.com
2 xyz xyz#aaa.com
Here's a nice tutorial
You can also do:
SELECT users.*, users_info.*
FROM users, users_info
WHERE users.id = users_info.id AND users.id = 43;
This means:
"Get me all the columns from the users table, all the columns from the users_info table for the lines where the id column of users and the id column of users_info correspond to each other"