MY USERID IS 1
TABLE users
USERID NAMES EMAIL
1 KAT 1#1.com
2 JOHN 2#2.com
3 FIK 3#3.com
TABLE comunity
COMUNITY_ID USERID MYFANS
1 1#FROM TABLE USERS 1#1.com
2 2#FROM TABLE USERS 1#2.com
3 3#FROM TABLE USERS 1#3.com
SELECT u.USERID, u.NAME FROM
users u
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c. MYFANS = '1#1.com'
with his example i only get users which have my email in column MYFANS. but i want to get also users which i have their emails in MYFANS Column
its like select myfans where my email is '1#1.com' and these emails which i have in myfans where my userid = '1'
Related
now here I have these tables, user can have more than category, and I want to show table like this
I've wrote this statement but It repeats user name with every category
SELECT
c.name
FROM
permission p
JOIN users u
ON u.id = p.user_id
JOIN category c
ON c.id = p.category_id
USER DATA TABLE // this table is what I want to get because every user have more than one category
username
email
categories
row
row
math, science
row
row
row, row, row
//those tables are what I have in the database
USERS TABLE
USER ID
username
email
5
row
row
6
row
row
CATEGORY TABLE
C ID
C NAME
8
math
9
science
PERMISSION TABLE
ID
USER ID
C ID
1
5
8
2
6
9
If you have only username use DISTINCT
SELECT DISTINCT
c.name
FROM
permission p
JOIN
users u ON u.id = p.user_id
JOIN
category c ON c.id = p.category_id
With more columns you should look for GROUP BY c.name
As user info in users table is unique so max keyword is used for optimization purpose. If same category is mapped multiple times with single user then use DISTINCT keyword inside GROUP_CONCAT functions. GROUP_CONCAT() is build in function for MySQL.
-- MySQL
SELECT MAX(u.username) username
, MAX(u.email) email
, GROUP_CONCAT(c.cname) categories
FROM users u
INNER JOIN permission p
ON u.userid = p.userid
INNER JOIN category c
ON c.cid = p.cid
GROUP BY u.UserID;
am covering my recently post
fetch results using join Mysql
i made some tries. on wat i want
TABLE users
USERID NAMES EMAIL
1 KAT 1#1.com
2 JOHN 2#1.com
3 FIK 3#1.com
4 PET 4#1.com
5 COW 5#1.com
TABLE comunity
COMUNITY_ID USERID FANS
1 1 KAT #FROM TABLE USERS 3#1.com # FIK IS MY FAN
2 2 JOHN #FROM TABLE USERS 1#1.com # AM FAN OF JOHN
3 1 KAT #FROM TABLE USERS 5#1.com # COW IS MY FAN
4 4 PET #FROM TABLE USERS 1#1.com # AM FAN OF PET
5 1 KAT #FROM TABLE USERS 2#1.com # JOHN IS MY FAN
i want to get all fans which have my email in fans column
i get them like this - this works
SELECT * FROM
users u
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c.FANS = 'MY USERID - 1'
now the problem is here
i want to also get users which i have they emails to fans column
i did it like this
select fans where my userid = '1'
so i need some correction here
SELECT * FROM
users u
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c.FANS = 'MY USERID - 1'
AND * IN(SELECT c.FANS FROM comunity WHERE c.USERID = 'MY USERID - 1");
You can try this Query:
SELECT * from
(
SELECT * from community where USERID=1 OR FANS='1#1.com'
) c
left join
users u
ON u.USERID = c.USERID;
I have two MySQL tables - 1) users and 2) warehouseMapping.
users
id, name
warehouseMapping
id, userId, warehouseId
From the above you can see that "warehouseMapping" table is the mapping table which will show that which warehouseIds are related to a particular user. There will be multiple warehouseIds for an user.
SELECT * FROM `users`
LEFT JOIN warehouseMapping
ON warehouseMapping.userId = users.id
WHERE 1 AND warehouseMapping.warehouseId IN (1, 2)
My intention is to show the users who have the warehouse id 1 and 2.
After submitting the same, MySQL showing me the same user name twice.
Inputs for users table:
id name
***************************
1 Niladri
2 Tanay
Inputs for warehouseMapping table:
id userId warehouseId
*****************************
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
If you want to find users who have both warehouses, here is one way:
SELECT u.id, u.name
FROM users u
LEFT JOIN warehouseMapping w
ON w.userId = u.id
WHERE w.warehouseId IN (1, 2)
GROUP BY u.id
HAVING COUNT(DISTINCT w.warehouseId) = 2;
For simplification, I have two tables related with one to many using a foreign key, for example:
Users table:
id
name
Actions table:
id
user_id
one user may have many actions or not. I need an sql select that returns users ids that don't have a user_id value in the actions table.
Users Table:
id name
1 John
2 Smith
3 Alice
Actions Table:
id user_id
1 3
2 1
So I need an sql query that returns the user id 2 (Smith) because the foreign key values don't include the id 2
I tried the following SQL, but it returns all users ids:
SELECT users.id from users left join actions on actions.user_id is null
select u.id
from users u
left outer join actions a on a.user_id = u.id
where a.user_id is null
Optimized version would be:
SELECT u.id
FROM users u
LEFT JOIN actions a
ON a.user_id = u.id
AND ISNULL(a.user_id)
SELECT u.id
FROM users u
LEFT JOIN actions a
ON a.user_id = u.id
WHERE a.user_id IS NULL
I need help querying the friendID from a table.
My table stores the user id of two members who are friends together.
But in order to store a "friendhship" b/w two members I would have to store two records like this:
friendshipID | userID | friendID
1 | 5 | 10
2 | 10 | 5
Yet, that seems heavy for the DB when we really only need to store the first record as that is sufficient as it contains both ids of both members.
However, the trouble comes when I want to query the records of the friends of ID=5. Sometimes the ID is in the userID column and other times it is in the friendID column.
This is the query I am using:
SELECT *
FROM friends
WHERE userID = '5'
OR friendID = '5'
But what I want to do is something like this
SELECT
if $userID=5 then userID as myfriend
else friendID=5 then friendID as myfriend
FROM friends WHERE userID='5' OR myfriendID='5'
Hope that makes sense. In the end I would like to have all the friends ID's of member #5 and not bring up results with #5 as the friend or user....but just his friends.
This query would return the Id value, and name, of the friends of #5 as shown in this SQL Fiddle Example
SELECT f.FriendId AS FriendId
, u.Name AS FriendName
FROM FriendTable AS f
INNER JOIN UserAccount AS u ON f.FriendId = u.UserId
WHERE f.UserId = 5
UNION
SELECT f.UserId AS FriendId
, u.Name AS FriendName
FROM FriendTable AS f
INNER JOIN UserAccount AS u ON f.UserId = u.UserId
WHERE f.FriendId = 5
The UNION will remove duplicates, making this query work for both a single record of friends, or the 2 record friendship you mention in the comment. You shouldn't need the 2 record friendship though, because there is no new information being stored in the second record that you cannot get from only having one record.