SQL intersect, union? - mysql

I have two tables EVENTS, and USER
EVENTS has fields "id", "name", "date"
USER has 1 field "id"
I want an SQL query to get all of the EVENT names of only the events that have the matching id's in USER. In other words, take all of the id's from USER, find the matching id's in EVENTS, display the name of the events that have those id's.
specifically, i want to display a list in html with all of these names
Thank you!

You would need to use an inner join for this, to do this your SQL should look something like this:
SELECT events.id, name, user.id
FROM events
INNER JOIN user ON events.id = user.id;
I would also advise against using block capitals for table names etc. Not that there is any technical reason for it, only it makes queries etc more readable.
Hope this helps :)

I want an SQL query to get all of the EVENT names of only the events
that have the matching id's in USER.
create table EVENTS (id number,name varchar2(20), date1 date);
create table user1(id number);
select u.id,e.name
from
USER1 u
INNER JOIN
EVENTS e
ON u.id = e.id;
In other words, take all of the id's from USER, find the matching id's
in EVENTS, display the name of the events that have those id's
select u.id,e.name
from
USER1 u
LEFT OUTER JOIN
EVENTS e
ON u.id = e.id;

Use exists:
select e.*
from events e
where exists (select 1 from user u where u.id = e.id);

U can use it directly using a WHERE Statement
select u.id,e.name
from
USER u,EVENTS e
where u.id = e.id;

Related

i have to write a MySql query to find out names of users who have organised events sorted in order

i have two tables user and events.
the query to be written is to find the names of users who have organized events
query 1:
select user.name
from user, events
where user.id = events.organiser_id
order by user.name;
query 2:
select name
from user
where id in(select organiser_id from events)
order by name;
why is it that "query 2" works fine but "query 1" doesn't? How to decide on when to use a subquery or a join?
A typical join syntax for mysql would be:
select distinct user.name
from user
join events ON events.organiser_id = user.id
order by user.name;
SELECT DISTINCT
U.Name
FROM USER U
LEFT JOIN
EVENTS E
ON U.id = E.organiser_id
WHERE E.organiser_id IS NOT NULL
ORDER BY U.Name
use JOIN to select all users and related events then filter with WHERE to exclude those users who do not have a link (an id in the event table); if there are more events for each user in the event table, then DISTINCT must be used (this in T-SQL)
The second query doesn't work because the column name doesn't match. You have one that says, "id in (select organiser_id". If you did this, it would work, "id in (select organiser_id as id"

Add to VIEW from another table

I need to find out how to pull user names into a VIEW from a different table. I have 3 tables, User, Lead, and Lead_detail. In the users table there is an ID field which is stored in the Created_By field in the Lead table. In the Lead table I have an ID field that is stored in the Lead_detail Lead_ID field.
I have created a VIEW to the Lead_detail table which pulls all the info I need but I have found that I don't have the users name in that VIEW so I need to ALTER my view to add in the users names per lead but I am having trouble with the statement.
Before altering the VIEW I wanted to try a SELECT statement to see if I get any data,
SELECT * FROM Lead_detail
JOIN Lead
ON Lead_detail.lead_id = Lead.id
WHERE Lead.Created_by = Users.ID
But this didn't work. What would be a correct statement so that I can pull the users names into the Lead VIEW?
I think you missed a join to the Users table:
SELECT
*
FROM
Lead_detail
INNER JOIN Lead
ON Lead_detail.lead_id = Lead.id
INNER JOIN Users
ON Lead.Created_by = Users.ID

Retrieving data from 3 Mysql tables

Suppose I have 3 different tables relationships as following
1st is tbl_users(id,gender,name)
2nd is tbl_feeds(id,user_id,feed_value)
3rd is tbl_favs(id,user_id,feed_id)
where id is primary key for every table.
Now suppose I want to get data where those feeds should come which is uploaded by Gender=Male users with one field in every row that should say either the user who is calling this query marked that particular feed as favourite or not.
So final data of result should be like following :
where lets say the person who is calling this query have user_id=2 then is_favourite column should contain 1 if that user marked favourite that particular feed otherwise is_favourite should contain 0.
user_id feed_id feed_value is_favourite gender
1 2 xyz 1 M
2 3 abc 0 M
3 4 mno 0 M
I hope you getting my question , I m able to get feeds as per gender but problem is I m facing problem to get is_favourite flag as per particular user for every feed entry.
I hope some one have these problem before and I can get help from those for sure.
I would be so thankful if some one can resolve my this issue.
Thanks
Something like this should work:
SELECT
u.id AS user_id.
fe.id AS feed_id,
fe.feed_value,
IFNULL(fa.is_favourite, 0),
u.gender
FROM
tbl_users u
JOIN
tbl_feeds fe ON (fe.user_id = u.id)
LEFT JOIN
tbl_favs fa ON (
fa.user_id = u.id
AND
fa.feed_id = fe.id
)
In order to link your tables, you need to find the most common link between them all. This link is user_id. You'll want to create a relationship between all tables with JOIN in order to make sure each and every user has data.
Now I don't know if you're planning on making sure all tables have data with the user_id. But I would use INNER JOIN as it will ONLY show records of that user_id without nulls. If the other tables could POSSIBLY (Not always guaranteed) you should use a LEFT JOIN based on the tables that is it possible with.
Here is an SQLFiddle as an example. However, I recommend you name your ID fields as appropriate to your table's name so that way, there is no confusion!
To get your isFavorite I would use a subquery in order to validate and verify if the user has it selected as a favorite.
SELECT
u.userid,
u.gender,
f.feedsid,
f.feedvalue,
(
SELECT
COUNT(*)
FROM
tbl_favs a
WHERE
a.userid = u.userid AND
a.feedsid = f.feedsid
) as isFavorite
FROM
tbl_users u
INNER JOIN
tbl_feeds f
ON
u.userid = f.userid
~~~~EDIT 1~~~~
In response to your comment, I have updated the SQLFiddle and the query. I don't believe you really need a join now based on the information given. If you were to do a join you would get unexpected results since you would be trying to make a common link between two tables that you do not want. Instead you'll want to just combine the tables together and do a subquery to determine from the favs if it is a favorite of the user's.
SQLFiddle:
SELECT
u.userid,
f.feedsid,
u.name,
u.gender,
f.feedvalue,
(
SELECT
COUNT(*)
FROM
tbl_favs a
WHERE
a.userid = u.userid AND
a.feedsid = f.feedsid
) as isFavorite
FROM
tbl_users u,
tbl_feeds f
ORDER BY
u.userid,
f.feedsid

MySQL using Group By to limit results

Okay, so I am trying to perform a query that has 4 tables,
users, events, event_roles, user_event_role.
The users can fill multiple roles. What i am trying to do is get a result that looks more like this:
User, event, Role(s)
So if user 'Bill' is associated with event 'Meeting' and 'Bill' Fills multiple roles instead of getting a result like this:
user event role
--------------------------
bill Meeting admin
bill Meeting director
how would I get my result to be like this
user event role role
----------------------------------
bill Meeting admin director
Here is a query that I'm trying to build off of.
Select *
FROM `users` u
LEFT JOIN `event_role` er ON u.user_id = er.user_id
LEFT JOIN `events` e ON er.event_id = e.event_id
The result you seek is not possible.
However there is something close:
SELECT
user,
event,
group_concat(role SEPARATOR ',') as roles
FROM
`users` u
LEFT JOIN `event_role` er
ON u.user_id = er.user_id
LEFT JOIN `events` e
ON er.event_id = e.event_id
GROUP BY u.user_id
which would yield:
user event roles
----------------------
bill Meeting admin,director
In either case you would need to adjust your logic to parse it correctly.
You cannot get such result, because you don't know how many roles there might be (i.e. columns count), but you can use GROUP_CONCAT that way:
SELECT *,
GROUP_CONCAT(event_roles.role SEPARATOR ',') as roles
FROM users
LEFT JOIN event_role USING(user_id)
LEFT JOIN events USING(user_id)
GROUP BY user_id
Using this query you will get all roles concatonated with ,. But be aware of limitation of GROUP_CONCAT, the default value is set to 1024 characters which might not be enough (see my.cnf).
Use Group_concat (http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html) like this.
To show that quickly, I'm using a database from the excellent MySQL intro book by Ben Forta (http://www.forta.com/books/0672327120/), no affiliation.
SELECT v.vend_id, GROUP_CONCAT(p.prod_id)
FROM products p
JOIN vendors v ON v.vend_id= p.vend_id
JOIN orderitems oi ON oi.prod_id = p.prod_id
GROUP BY v.vend_id;

SQL how to select from 2 tables and match them

I have table called users and table called events.
Every user sets is own preferred "area codes".
Every event is set to some area codes, and this information is saved in a table:
events_areas:
area_id BIGINT
event_id BIGINT
I am trying to find a good way to let the user select is own area codes... and then to match it in a select statement with the event area codes.
i tought about doing the same as events_areas and do: users_areas:
area_id BIGINT
user_id BIGINT
But then I dont know how to match them in select statement... ?
Thanks
Assuming the tables you listed, the following would select all events in the users selected area codes.
SELECT u.*, e.*
FROM users u
JOIN users_areas ua
ON u.id = ua.user_id
JOIN events_areas ea
ON ea.area_id = ua.area_id
JOIN events e
ON ea.event_id = e.id
The select statement would look something like this:
SELECT DISTINCT users.*, events.*
FROM users
JOIN users_areas ON users.user_id = users_areas.user_id
JOIN events_areas ON users_areas.area_id = event_areas.area_id
JOIN events ON events_areas.event_id = events.event_id
It's useful to start listing the relations between the tables (objects) in plain English. For example:
A user belongs to an area code and an area code has many users.
An event happens in an area code and an area code has many events.
Both statements describe many-to-many relations, so you do need those two tables (sometimes called cross-reference tables).
Writing those statements always helps to understand the problem and ask questions about it. Assuming the statements above are correct, then having those two tables users_areas and events_areas is correct too. To build the select statement, note that there's only one column that both tables have in common: area_id (which by the way, would fit in a int, using bigint is a huge overkill, and I think the same goes for user_id and event_id). So area_id is the column you need to use to match them (the correct term is to join them).
Here's your select statement:
SELECT ea.event_id, ea.area_id, ua.user_id
FROM events_areas ea
INNER JOIN user_areas ua ON ea.area_id = ua.area_id
Or, assuming you also have an event table and a user table, which is likely the case, we expand the select to look like this:
SELECT e.name, u.name
FROM events_areas ea
INNER JOIN user_areas ua ON ea.area_id = ua.area_id
INNER JOIN users u ON u.user_id = ua.user_id
INNER JOIN events e ON e.event_id = ue.event_id
Okay I changed this based on your comment...
Create these tables:
Table: user
id
name
Table: user_area
id
user_id
area_id
Table: event
id
name
Table: event_area
id
event_id
area_id
Table: area
id
area_code
Then run this query:
SELECT event.name FROM event, event_area WHERE event.id = event_area.event_id AND event_area.area_id IN (SELECT area_id FROM user_area WHERE user_id = <CURRENTUSERSID>)
SELECT *
FROM users u
INNER JOIN users_areas ua
ON u.user_id = ua.user_id
INNER JOIN events_areas ea
ON ua.area_id = ea.area_id
INNER JOIN events e
ON ea.event_id = e.event_id