I have a table, with rows of events, and each one has (amongst lots of other fields) addedbyuser, editedbyuser, deletedbyuser
There are INT, and refer back to the users table to a particular user.
I am able to join one of the fields (say addedbyuser) without any problems, how do i join the rest and reference them in php?
events table:
eventid addedbyuser editedbyuser deletedbyuser
1 1 2 3
users table:
id username
1 name1
2 name2
3 name3
So basically, I want to display the names of who added, edited and deleted the article, can I do this in one SQL query?
Something like this:
select
evn.eventid,
us1.username as addedbyuser,
us2.username as editedbyuser,
us3.username as deletedbyuser,
from events evn
join users as us1 on
evn.addedbyuser = us1.id
join users as us2 on
evn.editedbyuser = us2.id
join users as us3 on
evn.deletedbyuser = us3.id
Related
I have 3 tables :
Person table stores basic person wise details with ID as primary Key
This person can have relationships (father / mother etc), which are saved in Relationship table, however the users for them are created in Person table (e.g. ID = 2,3 in person table), This way we know that 2,3 are related to user 1 (carry).
We also have 3rd table - address, which store user ID wise addresses.(for both a user and his related persons, who are also users)
I want to find out if an address exists for either a user or for his related users in SQL. How to achieve this ?
You can combine two rules and search on the combined table as below
SELECT * FROM
(
SELECT username,id,Address.Address
FROM Person
INNER JOIN Address ON Person.id = Address.Userid
UNION ALL
SELECT username,id,Address.Address
FROM Person
INNER JOIN Relationship ON Relationship.Relatedid = Person.id
INNER JOIN Address ON Relationship.Userid = Address.Userid
) as RES
WHERE Address = 'xyz road'
Also you can find DBFiddle link to workout
Query:
select p.id,p.username,(case when a.userid is null then 'No' else 'Yes'end) IsAddressAvailable
from Person p
left join Address a on p.id=a.Userid
Output:
id
username
IsAddressAvailable
1
Carry
Yes
2
Carry-Father
No
3
Carry-Mother
Yes
db<fiddle here
i'm running into a complex problem, the data in a db has three tables.
First_DB
-- default_users
id username email password
1 Timbog Timbog#mail.com vads7y3kkjdfa
2 Marta Marta#mail.com vads7y3kkjdfa
-- default_album
album_id album_name default_user_id
1 Name_Tim 1
3 Katarina 2
-- default_album_img
img_id image_file album_id
3 1320229733.jpg 1
4 3320229733.jpg 3
Second_DB
--users
user_id user_name user_email user_pass user_image
1 Timbog Timbog#mail.com vads7y3kkjdfa 1320229733.jpg
2 Marta Marta#mail.com vads7y3kkjdfa 3320229733.jpg
The approach i used to solve this problem is to first fetch all data by inner join, should i use full outer join and insert the required field to my table, the following query is actual by which i'm trying to make it wor:
INSERT INTO bbpin.users ( user_name, user_pin, user_email, user_password, user_img)
SELECT default_users.username, default_users.bb_pin, default_users.email, default_users.password
FROM bbmpins_pins.default_users
INNER JOIN bbmpins_pins.default_album_images
ON default_album_images.album_id = default_users.id;
i miss the thing how do i compare two table's id in this join maybe? or this query is all wrong by approach?
By two tables which are sepearte in First_DB there could be multiple record how do we trunk them to last entry only ?
Thanks
It looks like you are attempting to retrieve all rows from the default_users table. And along with each row, also return the corresponding row(s) from default_album table. And along with that, the corresponding row(s) from default_album_img table.
Given the example data, a query using inner joins would return the specified result:
SELECT u.id AS user_id
, u.username AS user_name
, u.email AS user_email
, u.password AS user_pass
, i.image_file AS user_image
FROM default_users u
JOIN default_album a
ON a.default_user_id = u.id
JOIN default_album_img i
ON i.album_id = a.album_id
That query will work for the example data.
But, if there is a row in default_user which doesn't have a matching row in default_album, then an inner join won't return that row:
-- default_users
id username email password
3 branstark bran#winterfell warg2
Or, if there are two or more rows in default_album that match a given user, then the query will return two copies of the row from default_user...
-- default_album
album_id album_name default_user_id
1 Tim2 1
Without a specification of what is to be returned in those cases, we can't recommend a query.
I don't see anything wrong with your current approach using a JOIN but could modify it a bit to be more readable and also you will have to join the relation table
INSERT INTO bbpin.users (user_id, user_name, user_pin, user_email, user_password, user_img)
SELECT du.id,
du.username,
du.bb_pin,
du.email,
du.password,
dai.image_file
FROM bbmpins_pins.default_users du
JOIN bbmpins_pins.default_album da ON du.id = da.default_user_id
INNER JOIN bbmpins_pins.default_album_images dai
ON dai.album_id = da.album_id;
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"
I'm looking to join a 2 tables but the second table has a one to many relation. Can I omit the entire row if any of the lines have a certain value? Let me explain more.
User table
id name email
1 bob bob#test.com
2 foo foo#test.com
Music table
id userId
1 1
1 2
2 1
3 1
2 2
Say I don't want it to show the user if he has a relation to music table id 2. Also looking for distinct user.
If I try something like this it will still show both users.
SELECT * FROM users u LEFT JOIN music m ON u.id = m.userId WHERE m.id <> 3
I want it to check all the rows and if it has the id 3, it won't show. I hope I made sense. Thanks a lot.
Try using sub query like this:
SELECT * FROM users
WHERE id NOT IN (SELECT userId FROM music WHERE id=3)
This query means to select all users if their id is not related with music.id 3.
How can I create a view that merges different columns with a different table? I have three tables for example: users, items and gifts (in this example it's a system that a user can give a gift to another user)
users table has information about users, items table has information about items and gifts table shows which user sent what gift to which user.
What I want is to create a view like following:
user_from | user_to | gift_name | gift_price
sally | john | Teddy Bear | 10
You must join the three tables first. Example
CREATE VIEW GiftsList
AS
SELECT b.name user_from,
c.name user_to,
d.name gift_name,
d.price gift_price
FROM gift a
INNER JOIN users b
ON a.user_from = b.id
INNER JOIN users c
ON a.user_from = c.id
INNER JOIN items d
ON a.item = d.id
You can create a view with two tables like:
CREATE VIEW giftList AS
SELECT users.user_from,users.user_to,gifts.gift_name,gifts.gift_price FROM users,gifts
WHERE users.user_id = gifts.user_id;
The where clause is to make sure the output does not repeat.
I believe were looking for data blending. So basically having google data studio do a JOIN statement on ids from 2 data sets