I have three tables on my database:
ts_room
Fields:
id (PK)
capacity
img
notes
building_id
roomstructure_id
lecturestyle_id
ts_request
Fields
id (PK)
day_id
period_id
roompref_id (FK > ts_roompref.id)
ts_roompref
id (PK)
request_id (FK > ts_request.id)
room_id (FK > ts_room.id)
I would like to write a MySQL PDO query that selects rows from ts_room provided that after running the rows on ts_roompref (checking ts_roompref.room_id against ts_room.id) and finding a match - we would then look up the value in ts_request and see whether day_id and period_id both match 1. It would return the total count.
I hope the above explanation makes sense.
Essentially - requests for rooms are made with this system and ts_roompref stores the room preferences made for each request. I am trying to find out whether a particular room is booked on a day and period (denoted by day_id and period_id in the ts_request table), in this case Monday (1) and period (1).
how about this?
SELECT COUNT(*) totalCOUnt
FROM ts_room a
INNER JOIN ts_roompref b
ON a.id = b.room_ID
INNER JOIN ts_request c
ON b.request_ID = c.roompref_ID
WHERE c.day_ID = 1 AND c.period_ID = 1
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
Related
I'm building an activities website and I want to show on user's profiles the common activities I'm going to with this other user.
So I have a table as follow :
Every time a user clicks "join" on an activity page, a new row is added to this table with :
the user_id who joined
the activity_id (event) attended
So on each user's profile, I want to list the different activities we have in common.
If the user's IDs are for example 1 and 10, then I can list the activities they go to with this query :
SELECT * FROM `activity_user` WHERE user_id IN(1, 10)
However, how to update this query to return only activities IDs they have in common?
It's a simple JOIN with the same table:
SELECT a1.activity_id
FROM activity_user a1
JOIN activity_user a2 ON a2.activity_id = a1.activity_id
WHERE a1.user_id = 1
AND a2.user_id = 10
Another way is to GROUP BY activity_id and only return activities, which both users have, by counting the rows per group:
SELECT activity_id
FROM activity_user
WHERE user_id IN(1, 10)
GROUP BY activity_id
HAVING COUNT(*) = 2
If the number of user is only limited to 2, this is quite simple using intersect clause -
SELECT activity_id
FROM `activity_user`
WHERE user_id = 1
INTERSECT
SELECT activity_id
FROM `activity_user`
WHERE user_id = 10
For two users you can join two SELECT against same table on activity_id
SELECT a.activity_id
FROM test a
JOIN (SELECT activity_idFROM test WHERE user_id = 10) b ON a.activity_id = b.activity_id
WHERE a.user_id = 1
I have the following tables:
Users
user_id course_id completion_rate
1 2 0.4
1 23 0.6
1 49 0.5
... ... ...
Courses
course_id title
1 Intro to Python
2 Intro to R
... ...
70 Intro to Flask
Each entry in the user table represents a course that the user took. However, it is rare that users have taken every course.
What I need is a result set with user_id, course_id, completion_rate. In the case that the user has taken the course, the existing completion_rate should be used, but if not then the completion_rate should be set to 0. That is, there would be 70 rows for each user_id, one for each course.
I don't have a lot of experience with SQL, and I'm not sure where to start. Would it be easier to do this in something like R?
Thank you.
You should first cross join the courses with distinct users. Then left join on this to get the desired result. If the user hasn't taken a course the completion_rate would be null and we use coalesce to default a 0.
select c.course_id,cu.user_id,coalesce(u.completion_rate,0) as completion_rate
from courses c
cross join (select distinct user_id from users) cu
left join users u on u.course_id=c.course_id and cu.user_id=u.user_id
Step1: Take the distinct client_id from client_data (abc) and do 1 on 1 merge with the course data (abc1) . 1 on 1 merge helps up write all the courses against each client_id
Step2: Merge the above dataset with the client info on client_id as well as course
create table ans as
select p.*,case when q.completion_rate is not null then q.completion_rate else 0
end as completion_rate
from
(
select a.client_id,b.course from
(select distinct client_id from abc) a
left join
abc1 b
on 1=1
) p
left join
abc q
on p.client_id = q.client_id and p.course = q.course
order by client_id,course;
Let me know in case of any queries.
I have been struggling with this for several hours, so any feedback or advise is very welcome.
I have three tables:
users
id name email
1 test test#test.com
2 test2 test2#test.com
pets
pet_id pet_name user_id
1 sam 2
2 sally 1
transactions
trans_id custom
1 1
2 pid2
3 pid1
OK, what I would like to do is get transaction data relating to the user. So in the 'transactions' table 'custom' value 1 would relate to 'users' with the id. Thats the simple bit...
'Transactions' with 'pid' relate to the pets id, so 'pid2' relates to sally, whose user is user id 1. So I need to join the transaction table when custom relates to the user id or if its prefixed with 'pid' and the appending value relates to the 'pet_id'.
Here's an example of the result I would like:
Transactions relating to user_id 1:
trans_id 1, custom 1
trans_id 2 custom pid2 (this is because the pets owner is user_id 1)
Here is where I am with my attempt at the moment:
SELECT users.*, transactions.*
FROM users
LEFT JOIN transactions on users.id = transactions.custom
This is where I'm falling over:
SELECT users.*, transactions.*
FROM users
LEFT JOIN pets ON pets.user_id = user.id
LEFT JOIN transactions on (users.id = transactions.custom
OR pets.pet_id REGEXP '^pid(transactions.custom)')
If you can't change the table design and the prefix pid is fixed you could use
OR (
pets.pet_id = SUBSTR(transactions.custom, 3)
AND SUBSTR(transactions.custom, 1 FOR 3) = 'pid')
see documentation to SUBSTR and because MySQL automatically converts numbers to strings as necessary, and vice versa, see: http://dev.mysql.com/doc/refman/5.7/en/type-conversion.html
You HAVE to refactor Your DB. Current structure will guarantee of speed problems.
Table transactions should looks like
CREATE TABLE transactions
(
id Int NOT NULL, (id of transaction)
pet_id Int, (can be null)
user_id Int (can be null)
other columns here...
)
;
I have 2 tabes in my database:
users - userID(primary key), username, password
courses - id(primary key), name, text
subscriptions - id(primary key), curso_id, user_id
right in the subscription table I am writing the users UserID inside -> user_id with the id of the course for which he is subscribed curso_id so the results in the subscrption database are like
subscribtions table:
id user_id curso_id
1 12 1
2 5 1
3 12 2
4 6 7
this is the users table:
users table
userID username password
1 user1 passw1
2
3
4
and this is the courses table:
course table:
id course_name descriotion
1 course one text
2
3
My question is how to make a sql Query which first select the course by $row['id'] which indicates the id variable from the courses database, and after that based on the 3th table subscription to list all the users which are subscribed to this course number. ?
and second question is how to list number of the subscribed users for a course selected by
$row['id']
Here is some kind of the alghorytm logic that I have right now
SELECT * FROM users WHERE id=5.courses(select from the database 'courses') AND 5-> SELECT ALL FROM table subscriptions user_id equal to id from table users
Not sure if I understood what you try to achieve, but if you want a list that shows you every user in every course with the name of the course and ordered by course name, this should make the deal.
select t1.*, t2.*, t3.* from users as t1, courses as t2, subscriptions as t3 where t3.user_id = t1.userID and t3.curso_id = t2.id order by t2.id
Not the nicest way to do this and from a performance aspect I would recommend to put this in several statements as joins are usually slow.
okay I found the way and here is the mysql query:
FROM users
INNER JOIN subscriptions
ON users.userID = subscriptions.user_id
WHERE subscriptions.curso_id = $ids"
Let's say i've got a database with 4 tables:
users: (personal database)
id
user_name
lessons: (lesson names like math / gym etc.)
id
lesson_name
payments (contains payments with lesson_id and a part of the payment [the whole payment is divided to 12 parts])
id
lesson_id
part
value
groups: (to array users to lessons)
id
lesson_id
user_id
My question is how to query the database to get a table with list of all people attending to for ex. math (or any other lesson_id) with their name (probably LEFT JOIN) and SUM of all the payments for each period. The table header for chosen lesson_id should look like:
username | payment 1 | payment 2 | ...(3-11)... | payment 12
I have no clue how to make this work. Maybe my MySQL approach is innapropriate or i should divide it to more queries for more flexibility.
Thank You for your time!
select users.id, users.user_name,lessons.id, lessons.lesson_name,
(SELECT SUM(value) FROM payments
WHERE payment.lesson_id = lessons.id)
FROM users
INNER JOIN groups ON (groups.user_id = users.id)
INNER JOIN lessons ON (lessons.id = groups.lesson_id)
WHERE .....