I have three table,table names are user,trade,trade_photo.Here I have attached the table.
my problem is,
I want to select the trade table_table image data.
how to select the image based in user_id?
please any one help to me?
something like this
select * from user u,trade t, trade_photo tp where u.user_id=t.user_id and t.trade_id=tp.trade_id;
if you want to select for a particular user
select * from user u,trade t, trade_photo tp where u.user_id=some_id and u.user_id=t.user_id and t.trade_id=tp.trade_id;
There is no need to join user table since user_id is already in the trade table.
Whith the supplied information
user
user_id
trade
user_id
trade_id
trade_photo
image
trade_id
You can try something as
select
u.user_id ,
t.image
from
trade_photo tp
inner join trade t on t.trade_id = tp.trade_id
inner join user u on u.user_id = t.user_id
where u.user_id = '{some user id}'
Its always better to provide some sample data and expected output, so
that a proper suggestion could be provided.
Related
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
I have 2 tables as shown below and I want to search users who have vehicle "motorbike" AND "car", related to the tables, the search result should show only (1) john , not (3) mark, because I want "AND" not "OR".
How is it done with MySQL query ?
select * from members m left join properties p on m.user_id = p.user_id where concat_ws(',',property,value) in ('vehicle,motorbike','vehicle,car')
try this:
select name from members m, properties p where m.user_id=p.user_id and p.value="moterbike" and p.value="car";
Use the HAVING clause:
SELECT
A.user_id,
name,
property
FROM members A
INNER JOIN properties B
ON A.user_id=B.user_id
WHERE value IN ('motorbike','car')
GROUP BY A.user_id,name,property
HAVING COUNT(A.user_id)<>1
with respect,
I think some time it gives wrong result if we use having example because some time value can have 2 or more cars in same user
join example not retrieving any record
my answer is this
SELECT cc.*,mem.name
FROM
(SELECT user_id
FROM propoties
WHERE value='car') cc
JOIN
(SELECT userid
FROM properties
WHERE value='moterbyke') mb ON cc.userid=mb.userid
JOIN member mem ON cc.memberid=mem.memberid
Here is my query: I want to get the user values which is stored in user table and comments which is stored in comments table.
The user_id is the id of the user which i fetch from comments table.
How to get both comments with respective users how to do that with single query or there is any good and best way to do that?
mysql_query("select profile_pic,firstname,lastname,username,comment from user join comments where user_no IN (select user_id,comment from comments where question_id='".$_POST['get_comments']."') ");
Try
SELECT profile_pic,firstname,lastname,username,comment
FROM `user`
INNER JOIN comments ON `user`.user_no = comments.user_id
WHERE comments.question_id='".$_POST['get_comments']."'
i think the join condition you didn't specify and also you didn't specify what type of join you required try this code.
mysql_query("select profile_pic,firstname,lastname,username,comment from user left join comments on user.columnname=comments.columnname where user_no IN (select user_id,comment from comments where question_id='".$_POST['get_comments']."') ");
The column name specifies on which column you want to join to tables.for reference please read this
Im Running into walls with this one but im sure somebody here knows a way around
I have 2 tables for example USERS and ISSUES. In USERS they have we say 2 columns needed
id
firstname
lastname
in 'ISSUES` they have 2 columns needed
id
assigned_to_id
Im trying to compare and replace a field on output.
for example if users.id = issues.assigned_to_id
then print 'users.firstnameinstead ofusers.id`
Any help would be fantastic. cheers guys.
EDIT; i can do two queries to what i need, the first one is this;
SELECT assigned_to_id AS Name, COUNT(*) AS Issues FROM issues
WHERE `status_id`=1
OR `status_id`=2
OR `status_id`=4
OR `status_id`=7
OR `status_id`=8
OR `status_id`=9
OR `status_id`=10
GROUP BY `assigned_to_id`
and this one;
SELECT id, firstname FROM users
now if users.id = issues.assigned_to_id basically use the corresponding users firstname instead of the id
I don't see an issues.firstName column in your example so I assume you mean users.firstname
So to put into plain English what your after...
You want to return all issues and when an issue matches a user return the user name instead of the issueID?
If so then this should do it (Changed left to right as you wanted all issues I believe)
SELECT coalesce(u.FirstName, to_char(I.ID)) as UserNameOrIssueID
FROM Users U
RIGHT JOIN issues I
on U.ID = I.assigned_to_ID
The tricky part here is that firstname and ID are likely of different data types, so you have to cast the user.id to a character field in the DB appropriate syntax...
Using explicit cast (I'm also assuming Issue.ID is a numeric field if it's character then the second one below will work fine as it doesn't even need to do the implicit conversion.
SELECT coalesce(u.FirstName, cast(I.ID as char(30)) as UserNameOrIssueID
FROM Users U
RIGHT JOIN issues I
on U.ID = I.assigned_to_ID
Hoping implicit works:
SELECT coalesce(u.FirstName, I.ID) as UserNameOrIssueID
FROM Users U
RIGHT JOIN issues I
on U.ID = I.assigned_to_ID
I do not know, if this is what you need:
select
u.firstname
from
users as u,
issues as i
where
u.id = i.assigned_to_id;
You can not do that ... Only if you Alter table issues...
all you can do is this !
SELECT U.firstname FROM USERS U
LEFT JOIN ISSUES I ON U.id = I.assigned_to_id
I'll pus an update later ... with the alter table ! Stay tune !
UPDATE
IF OBJECT_ID ('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable
SELECT U.firstname INTO #TempTable FROM USERS U
LEFT JOIN ISSUES I ON U.id = I.assigned_to_id
SELECT * FROM #TempTable
-- ALTER TABLE ISSUES ADD firstname varchar(100) -- RUN ONLY ONCE !
UPDATE I
SET firstname = (SELECT TOP 1 U.firstname FROM USERS U
LEFT JOIN ISSUES I ON U.id = I.assigned_to_id)
FROM ISSUES I
SELECT * FROM ISSUES
I had a look over the weekend, this is what worked and did what i needed it to do. thanks for your help guys.
SELECT COUNT(*) as Issues, assigned_to_id as UserID , users.firstname as Name FROM issues
JOIN users on users.id = issues.assigned_to_id
WHERE `status_id`=1
OR `status_id`=2
OR `status_id`=4
OR `status_id`=7
OR `status_id`=8
OR `status_id`=9
OR `status_id`=10
GROUP BY `assigned_to_id`
ORDER BY Issues DESC
I have a table called bans where I have the follow fields:
room_id, banned_user_id, banned_by_id, reason, ts_start, ts_end
The users data come from the table called users, now I wanted to query the bans to retrive the name of who was banned and by who along with reason, time the ban was placed and time it ends.
So I have this query:
SELECT u.username, us.username, b.reason, b.ts_start, b.ts_end
FROM `bans` b
LEFT JOIN users us ON b.banned_by_uid = us.uid
LEFT JOIN users u ON b.banned_uid = u.uid
WHERE room_id = 3
My question here is wether my query is ok by using the LEFT JOIN for the 2 data I have to grab from the table users or there is a different approach for this kinda of scenario ?
Your query is perfectly acceptable. Each join to users is on a specific ID, which translates into a simple lookup, with minimal overhead.