I have 3 tables:
Table 1: columns are:
+------+-------+----------+----------+
| date | time | user_id | channel |
+------+-------+----------+----------+
Table 2:
+---------+------------+
| user_id | id _number |
+---------+------------+
Table 3:
+---------+-------+
| channel | sort |
+---------+-------+
In table 1 the columns are sorted as following :
2011-07-29, 12:35:15.650, 22, DeluxeMusic
In table 2 :
130.83.10.c42ce82365b9f6d , 22 (same as user_id in table 1)
In table 3:
DeluxeMusic (same as in table 1), entertainment.
The columns user_id in table 2 : 130.83.10.c42ce82365b9f6d means a user_id of some user. From this user ID I need to get all entries with the value of 130 in the begining. For all entries with same value 130.
Then I need to seek for this value of 130 in the table 3 the sort of channel they are watching, and to count by that all users watching a channel typ.
Channel typs are also : sport, shopping, entertainment and so on.
you could try this too:
select count(*) from t1 join t2 on t1.id = t2.id and t2.userid like '130%' join
t3 on t1.channel = t3.channel group by t1.channel
sql fiddle here
Edit:
another version:
select count(t1.id) , t1.*, t2.*, t3.*
from t1 join t2 on t1.id = t2.id join
t3 on t1.channel = t3.channel and t2.userid
like '130%' group by t3.channel, t1.id
fiddle
Not sure if I get your question completely. But from what I understand you would be needing something like below:
SELECT COUNT (*) FROM TABLE1 T1,TABLE2 T2,TABLE3 T3
WHERE T1.USER_ID = T2.USER_ID
AND T1.USER_ID LIKE '130%'
AND T1.CHANNEL = T3.CHANNEL
The above sql would be the used to fetch the count of the channels that the users with their use ids beginning with '130' would be watching.
Hopefully this should help you in someway.
Related
I have 3 tables:-
table1 :-
ReportType | ResourceId
t2 123
t3 5
table2:-
Id | Name | Created
1 A 10
2 B 11
123 C 12
table3:-
Id | Name | Created
4 D 13
5 E 14
6 F 15
table1's ResourceId and table2 and 3's Id column have same values
I want to create a 4th table like this:-
ReportType | ResourceId | Name | Created
t2 123 C 12
t3 5 E 14
such that wherever table1's ReportType is t2 I want the Name and Created value from table2 for the condition table1.ResourceId = table2.Id and wherever table1's ResourceType is t3 I want the Name and Created value from table3 for the condition table1.ResourceId = table3.Id.
PS: This isn't some sort of HomeWork. I have been stuck at this query for the past 1 hour, I have read various answers and tried a few queries of my own before posting the question. Any help would really be appreciated.
Explanation in comments :)
--here we join first and second table, but we filter results to include only ReportType = t2
select t1.ReportType, t1.ResourceId, t2.Name, t2.Created from table1 as t1 join table2 as t2 on t1.ResourceId = t2.id
where t1.ReportType = 't2'
union all
--here we join first and third table, but we filter results to include only ReportType = t3
select t1.ReportType, t1.ResourceId, t3.Name, t3.Created from table1 as t1 join table3 as t3 on t1.ResourceId = t3.id
where t1.ReportType = 't3'
You can use the below query:
select report_type, resourceid,name, created from dbo.t2, dbo.t1
where report_type='t2' and ResourceId=id
UNION
select report_type, resourceid,name, created from dbo.t3, dbo.t1
where report_type='t3' and ResourceId=id;
I need to find all attributes & image(s) for a product.
Every product is marked as such in the column type in Table 1.
The attributes are also defined in Table 1, each attribute on a seperate row.
This is a simplified version of the table structures:
Table 1:
id | type | value | url
1 | product | T-shirt |
2 | image | | http://www......
........
15 | size | XXL |
........
18 | color | blue |
Table 2:
id | ref_id | link_id
1 | 1 | 1
2 | 1 | 2
3 | 1 | 15
4 | 1 | 18
The relationship between a product and its attributes is defined in Table 2:
- ref_id matches the id in Table 1 for the product we want
- link_id holds an id (from Table 1) which is either the product itself or an attribute of the product
So in this example, id 1 in Table 1 has 4 occurences in Table 2 (ref_id = 1): the product row and 3 attribute rows (image, size & color)
They are referenced by the column link_id in Table 2.
I would like to solve this in 1 query if possible, but I am stuck at how to solve the 'back-reference'.
Once I have found a product's id in Table 1, I can get the link_id's in Table 2 allright, but how do I use those again (in the same query) to get the rows from Table 1.
Is this at all possible in 1 query, or should I use 2 seperate queries for this?
UPDATE:
this is how far I have come:
SELECT t1.id, t1.type, t1.value, t1.url,
t2.link_id
FROM table_1 t1
LEFT JOIN table_2 t2
ON t1.id = t2.ref_id
WHERE t1.type = 'product'
But after that I don't know how to construct the rest of the query based on the 'back-reference'.
You can join back to table_1 again
SELECT t1.id, t1.type, t1.value, t1.url,
t2.link_id, t1_attributes.type, t1_attributes.value
FROM table_1 t1
LEFT JOIN table_2 t2
ON t1.id = t2.ref_id
LEFT JOIN table_1 t1_attributes ON t2.link_id = t1_attributes.id
WHERE t1.type = 'product'
This will get you 1 row for each attribute though. If you want a field for each type of attribute, you'll have to do a join for each one.
SELECT t1.id, t1.type, t1.value, t1.url,
t2.link_id, t1_image.value, t1_size.value
FROM table_1 t1
LEFT JOIN table_2 t2 ON t1.id = t2.ref_id
LEFT JOIN table_1 t1_image ON t2.link_id = t1_image.id
LEFT JOIN table_1 t1_size ON t2.link_id = t1_size.id
WHERE t1.type = 'product' AND t1_image.type = 'image' AND
t1_size.type = 'size'
I have a table follow. I need to delete duplicate records based on two columns user_id and follow_id please tell me a query for delete
example:-
id | user_id | follower_id |
148 | 3 | 31 |
163 | 3 | 31 |
This query should identify all duplicates and remove all of them, remaining a single instance from each duplicate set, having the smallest id.
delete
from yourtable t1, yourtable t2
where (t1.user_id = t2.user_id) and (t1.follow_id = t2.follow_id) and (t1.id > t2.id)
This query worked.
select s.id, t.*
from follow s
join (
select user_id, follower_id, count(*) as qty
from follow
group by user_id, follower_id
having count(*) > 1
) t on s.user_id = t.user_id and s.follower_id = t.follower_id
I have a table as below
ID | CID
1 | 3
2 | 0
3 | 4
4 | 0
5 | 0
6 | 3
Below is the SQL query I use which is SELF JOIN.
SELECT t1.ID
FROM `tbl_a` AS t1 JOIN `tbl_a` AS t2
ON t1.ID = t2.CID
Which gives me O/P as below.
ID | CID
3 | 4
4 | 0
But what I want as an O/P is 1,3,4,6.
Logic of the O/P is Rows IDs or CIDs which are being used. If I explain more When the ID is 1 CID 3, When the ID is 3 CID is 4, When the ID is 6 CID is 3. When I get the unique IDs & CIDs that are used in the table would be 1,3,4,6.
Final Correct O/P Required is below.
ID
1
3
4
6
How can I get it done?
Not sure what you're trying to do. I think you are saying you want the ID of rows that have a non-zero CID or that are referenced by the CID column. (?) Try this:
SELECT ID FROM tbl_a AS t1 WHERE CID <> 0 OR EXISTS(SELECT * FROM tbl_a AS t2 WHERE t2.CID = t1.ID) ORDER BY ID
Try this
SELECT t2.ID
FROM `tbl_a` AS t1 JOIN `tbl_a` AS t2
ON t1.ID = t2.CID
OR t2.ID = t1.CID
GROUP BY t2.ID
I think this may be what you want:
select ID
from tbl_a
where id in (3, 4) or cid in (3, 4);
My SQL Skills are next to none. After looking around for the past 2 hours trying to figure this out I need some help please.
I have 2 tables as below
Table1 Table2
ID | Name Status_id
----------- ----------
1 | Open 1
2 | Closed 2
3 | On-Hold 1
What I would like to do is count the status_id in table 2 and group by the status_id. Then add the Name where the ID matches in the first column.
What I have at the moment is
SELECT status_id, COUNT(*) AS 'num' FROM table2 GROUP BY status_id
This is great so far and returns
1 | 2
2 | 1
What I need to return is
Open | 2
Closed | 1
I hope that is clear. Can anyone help?
Many thanks!
SELECT a.name, COUNT(*) AS num FROM table2 b
INNER JOIN table1 a
ON b.status_id=a.id
GROUP BY status_id
In the case that you want to also have Zero for On-Hold you'd need to do a LEFT join and count the a column from table2 instead of *
SELECT t1.name,
Count(t2.Status_id) AS num
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.Status_id
GROUP BY t1.name;
DEMO