right now im working on a query and for my brain this is an overload since i just started php and mysql.
I have 4 tables "Seminar, subscription, unsubscription, user" and i need a little data from all of them.
"Seminar, title", "subscription, active, datetime", "user, firstname, lastname" and "unsubscription, reason" Now I have only 1 value that i can use to get all this data and this is the seminar ID now i was wondering if that is enough to get the data from all tables.
Im really lost I hope someone can help me through this mess.
Thanks in advance
I got a little knowledge from inner joins, left joins etc.
Here is my ERD
http://img594.imageshack.us/img594/8923/erdk.png
How does this look? Typed on ipad so sorry for typos.
Select
Sem.title,
Sub.active,
Sub.datetime,
U.firstname,
U.lastname,
Unsub.reason
From
seminar sem
Inner join Subscription sub on sub.seminar_id = sem.id
Inner join user u on sub.user_id = u.id
Left join unsubscription unsub on sub.Id = unsub.subscription_id
Where
Sem.id = your-search-value-here
I'm assuming there won't always be an unsubscription, so this is a left join on to that table.
All others are inner... Meaning a subscription isn't complete without a user AND a seminar.
But If you want details about a seminar, regardless of if a user is subscribed, then your join to user should be changed from INNER to LEFT too.
And If a seminar can exist without ANY subscriptions, and you want such seminars included in the results, you should also change the join on subscriptions to be LEFT.
Note that Any fields selected from a left joined table, where no result was acquired on that table for that seminar, will be null.
Related
I am a beginner in MySQL, I had some courses in mysql that are now finished, I have a homework , provided by our teacher, to make for my training but I block to retrieve data from the base for a social network site. I understand the basis of JOIN but I still have trouble understanding the logic of LEFT JOIN OR OTHER type INNER JOIN ...
Here is my problem, I have a database with 2 tables,
member(id_member*, login, photo)
friend(id_member_request*, id_member_accept*, accept, date_acceptation)
The accept field of the friend table is a field that allows me to validate if they have friends by setting the value to 1 instead of 0.
The fields id_member_request and id_member_accept agree to the id_member of the member table.
I want to retrieve the login and the picture of the members who are friends, to be able to display them then.
I tested several queries:
SELECT m.login
, m.photo
FROM friend AS a
LEFT
JOIN member AS m
ON m.id_member = a.id_member_accept
LEFT
JOIN member AS m1
ON m1.id_member = a.id_member_request
WHERE accept = 1;
The query works but does not show all friendly members and even several times some people.
In RIGHT JOIN The result is NULL.
In INNER JOIN no result.
Thanks in advance because I blocked for several hours and I confess to lose myself a little. ;-)
Formatting the statement will help you :)
Okay, so you are joining the same column on both m and m1.
The following will bring back the data, but you have to pass in id_member_request..
DECLARE INT #MEMBERID = 2; --Example ID
SELECT m.login, m.photo
FROM friend AS a
LEFT JOIN member AS m ON m.id_member = a.id_member_accept
WHERE a.id_member_request = #MEMBERID
AND a.accept = 1;
This is bring back the m.login, m.photo for all of the friends for the Member with the ID #MEMBERID;
I think you want the friends of person who logged in. means for particular Member_Id here is query that will help you
select * from friend a
inner join member b on (a.id_member_request=b.id_member or a.id_member_accept=b.id_member) and b.id_member=1
where accept=1
where 1 is a member id whose friends will be displayed
You could re-write your query as in the below example
SELECT `m`.`id_member` AS `memberID`,
`m`.`login` AS `memberLoginName`,
`m`.`photo` AS `memberPhoto`,
`m1`.`id_member` AS `friendID`,
`m1`.`login` AS `friendLoginName`,
`m1`.`photo` AS `friendPhoto`,
`a`.`accept_date` AS `acceptDate`
FROM `friends` AS `a`
INNER JOIN `member` AS `m` ON `a`.`id_member_request` = `m`.`id_member`
INNER JOIN `member` AS `m1` ON `a`.`id_member_accept` = `m1`.`id_member`
WHERE `a`.`accept`=1
ORDER BY `a`.`id_member_request`,`a`.`id_member_accept` ASC
View this example in SQL fiddle
Thanks a lot for your help ;-)
I've tried all your answers and with a little change, this one works fine even in INNER, LEFT, RIGHT JOIN...
SELECT login,photo
FROM friend AS a
INNER JOIN member AS m ON ( a.id_member_request = m.id_member
OR a.id_member_accept = m.id_member )
WHERE m.id_member !=$id_member
AND a.accept=1;
Just looking for some help with this, i'm sure it is incredibly simple but after so many hours doing other areas of my site, i'm going a bit batty.
I just have a gaming competition whereby I have a table called 'leaders' that has only these columns:
fk_memberid | points_total
Quite simple. Then I have this query I found elsewhere on this forum to just get the rankings of each member.
SELECT
fk_memberid,
points_total,
(SELECT COUNT(*)+1 FROM leaders WHERE points_total>x.points_total) AS rank_upper,
(SELECT COUNT(*) FROM leaders WHERE points_total>=x.points_total) AS rank_lower
FROM
`leaders` x
My question is, how do I link the fk_memberid column to another table called "members" to the corresponding column "k_memberid"? I do this all the time of course but for some reason i'm struggling in this case due to the different type of query i'm familiar with above.
Sorry for the likely incredibly easy answer. Appreciate the help.
A quick example here:
SELECT l.fk_memberid, l.points_total, m.first_name FROM leaders l
left join member m
on m.k_member_id=l.fk_member_id
WHERE ...
This will give you back a table with 3 columns, 2 from leader table and "first_name" (assuming it exists) from member table
Do one thing
SELECT * FROM Table1 T1, Table2 T2
WHERE T1.column_name = T2.column_name AND Another_comditio(if you want);
may be it will help you
SELECT *
FROM leaders
LEFT JOIN members ON members.k_memberid = leaders.fk_memberid
I have a database with users. Now I'm trying to create a wall (Facebook like).
I've added a table in my db called status_update and added a row username(user who posted the status). Then I created a query to search for statuses from the user on whose profile page you're on (select from status where username=$_GET['profile']).
I'm wondering how smart this is? Is there a way to use JOIN or something? Now this was the only logic solution that came to my mind and I know how to make it.
Also this is fine for this page but when I'll create a feed page for those who are "Friends or Followers" that's gonna be a bit tricky.
Is there a better solution for this?
yes you can join two tables by there index or any value, just do it like this
select * from table1 inner join table2 on table.field=table.field
it depends what join you want, inner join if data present on both table other wise left if data may or may not present
Just getting to grips with some more advance sql queries and a bit stuck with one of them. I’ve been working on a small cinema project that allows user to book seats for upcoming productions.
In a table for chairs, I’ve created a query so that checkboxes are created on all of the chairs within a particular cinema area. However, I now need to add some sort of functionality to show chair availability i.e. whether one has already been booked by a previous order.
select * from `chairs` c
left outer join `booki1` b on b.`seatno`=c.`seatno`
left outer join `shows` s on s.`ddate`=b.`ddate`
left outer join `area` a on a.`name`=c.`area`
where (
c.`area` like '{$_SESSION['Area']}' and
s.`ddate` like '{$_SESSION['DDate']}' and
s.`ttime` like '{$_SESSION['TTime']}}'
);
Very hastily put together and totally untested, but you need to join the tables using suitable keys.
I am building an online survey system for which I wish to produce statistics. I want query based on the gender of the user. I have the following tables:
survey_question_options
survey_answer
users
I have constructed the following query so that it brings back a null response where there are no answers to the question:
SELECT COUNT(sa.option_id) AS answer , so.option_label
FROM survey_answer sa
RIGHT JOIN survey_question_options so
ON sa.option_id = so.option_id AND
sa.record_date>='2011-09-01' AND
sa.record_date<='2012-08-01'
LEFT JOIN users u
ON (sa.uid = u.uid AND u.gender='F')
WHERE so.question_id=24
GROUP BY so.option_label
ORDER BY so.option_id ASC
My query returns the following results set:
0 Red
1 Yellow
0 Blue
0 Green
However, the gender condition in the LEFT JOIN appears to be ignored in the query. When I change the gender to 'M' the same result is returned. However, the expected result would be 0 for everything.
I am not sure where I am going wrong. Please help.
Thanks in advance.
Well, you are doing a COUNT on a column from the main table, so the gender condition on the LEFT JOIN won't affect the result. You should do the COUNT on a column from the users table. I'm not sure if this is what you want, but you should try:
SELECT COUNT(u.uid) AS answer , so.option_label
FROM survey_answer sa
RIGHT JOIN survey_question_options so
ON sa.option_id = so.option_id AND
sa.record_date>='2011-09-01' AND
sa.record_date<='2012-08-01'
LEFT JOIN users u
ON (sa.uid = u.uid AND u.gender='M')
WHERE so.question_id=24
GROUP BY so.option_label
ORDER BY so.option_id ASC
The left join to the users table is evaluated after the join to the answer table - so although the user record is not returned if the user is the wrong gender, the answer record will be returned (regardless of the user's gender). Try:
SELECT COUNT(sa.option_id) AS answer , so.option_label
FROM (select a.option_id
from survey_answer a
JOIN users u ON a.uid = u.uid AND u.gender='F'
where a.record_date>='2011-09-01' AND
a.record_date<='2012-08-01') sa
RIGHT JOIN survey_question_options so
ON sa.option_id = so.option_id
WHERE so.question_id=24
GROUP BY so.option_label
ORDER BY so.option_id ASC
You're putting your condition in the wrong block. Since you're performing a LEFT JOIN, (which is a left-bound outer join) everything in the left table (the main table) is selected, together with the data from the joined table, where applicable. What you want is to add the data from all users and then restrict the full output of the query. What you've actually done is add the user data from only the female users and then displayed all data.
Sounds technical, but all you have to do is move the AND u.gender='F' into the main WHERE clause instead the ON clause. That will cause SQL to only select the rows for female users after the JOIN has taken place.