Hi guys im working on a problem. i have 2 tables, table a contains a list of users table b contains a list of their "clock ins", at the moment im selecting distinct dates from table b and then for each date, im checking if the user has checked in for that date. BUT what i want to be able to do is select some data from the Table B as well .. but when i try i just get spammed with all the data from table B. here is my statement.
SELECT *
FROM Table A,
Table B
WHERE EXISTS (SELECT *
FROM Table B
WHERE Table B.user_id = Table A.id
And Table B.date = 'given date here')
I still don't understand why you want to select data that is not there, but if you still have to, then I would take your schema as-
USER
-----------------
user_id
username
...
...
USER_CLOCKIN
-----------------
user_id
clockin_datetime
...
...
I would query this as-
SELECT u.*, uc.*
FROM user u LEFT OUTER JOIN user_clockin uc
ON u.user_id = uc.user_id
WHERE uc.user_id IS NULL
;
This selects all the columns from both the tables. Modify your SELECT list as per your requirement.
Try running following:
SELECT table_a.name,table_b.date FROM table_a LEFT JOIN table_b ON table_a.id=table_b.id WHERE table_b.date=<xx.yy.zzzz>
of course replace xx.yy.zzzz with date wanted. also, visit http://www.mysql.org go to documentation, and find more info on join, left join and right join...
Related
I'm struggling to write a SELECT query in my PHP application. The situation is following:
I have table ADDRESS with one-to-many relation to table CONTEST. The relation is via junction table ContestHasAddress. CONTEST table has many-to-many relation to DATE table via junction table ContestHasDate.
I was trying to SELECT all columns from ADDRESS table WHERE the column1 from DATE related to its CONTEST (who is related to the ADDRESS) is bigger then a specific VALUE. I have attached the linke below showing the relation view from phpmyadmin.
I tried that but it was a failure
SELECT address.*
FROM address,
contest,
date
WHERE address.contest.dates[0].start_time > TIMESTAMPVALUE
You need to use JOINs to establish all of the table relations to get to the Date table. Then, you can compare the values:
Select A.*
From Address A
Join Contest_Has_Address CA On A.Id = CA.Address_Id
Join Contest C On C.Id = CA.Contest_Id
Join Contest_Has_Date CD On C.Id = CD.Contest_Id
Join Date D On D.Id = CD.Date_Id
Where D.Start_Time > TIMESTAMPVALUE
You need to join the tables using the "junction table". I have assumed that Startime is DateTime field and not Int.
Select address.* from Address inner join ContestHasAddress
on Address.ID = Contest_Has_Address.address_ID INNER JOIN
Contest_Has_Date on Contest_Has_Address.ContestID = Contest_Has_Date.contestID
inner join date on Contest_Has_Date.DateId = Date.ID
where StartTime > TIMESTAMPVALUE
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
I have to tables: user and userimages.
Right now, I'm using the following query:
SELECT * FROM users u INNER JOIN userimages ui ON u.id=ui.userid;
Yes. It retrieved the records if the userid and id is equal. What I really want to do is to select all records from table users even if its id is not present on the second table (userimages)
My question is how to select all records in the users table with or withou userid on the second table?
You're looking for a LEFT JOIN, which will pull all rows from the first table and join where applicable to the right.
SELECT * FROM users u LEFT JOIN userimages ui ON u.id=ui.userid;
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 want to get numOfItem from table BUY using ticketTypeId and then using the BUY.userId to find in the table USER to get the gender. So I can get numOfItem from table BUY and gender from table USER. I don't know how to write this in one query. Any idea?
table structure:
TABLE BUY:
ticketTypeId
numOfItem
userId
TABLE USER:
gender
You need to join your tables on a common field, in this case user id
Select b.ticketTypeId, b.numOfItem, b.userId, u.gender
From buy b inner join user u on b.userid = u.userid
Where b.ticketTypeId = <val>
You want to include where to get only needed ticketTypeId
Generally speaking a join between two tables is something like:
select table1.*,table2.*
from
table1
join table2 on table1.key=table2.key
Add userId in the table user
join the tables with inner join in the select statement
select a.*,b.* from [user] a inner join [buy] b on a.userid = b.userid
You need to use a join. Here is an link
SELECT tb1.ticketId, tb1.numOfItem, tb1.userId, tb2.gender
FROM Table1 as tb1
JOIN Table2 as tb2
ON tb1.userId = tb2.userId