I have 3 tables in my Mysql DB as follows:
daily_data_update:
----------------------------------------------
id date code user_id followup_status
----------------------------------------------
1 08/10/2020 AD123 1 1
2 08/10/2020 AD134 2 2
3 08/10/2020 AD123 1 1
---------------------------------------------
users:
-------------------------------------
id first_name user_region
------------------------------------
1 Peter scotland
2 Susan ireland
------------------------------------
followupstatus
------------------------------------
id followup_status
---------------------------------------
1 Paid
2 Unpaid
----------------------------------------
I need to select date,code and followup_status from daily_data_update WHERE user_id in users along with followup_status. My query is as follows:
SELECT t1.`date`,
t1.`code`,
t3.`followup_status`
FROM `daily_data_update` AS t1,
followupstatus AS t3
LEFT OUTER JOIN users AS t2 ON t1.user_id = t2.id
WHERE t2.id IS NOT NULL
AND t2.user_region = 'scotland'
But the query is failing as Unknown column 't1.user_id' in 'on clause'
What is wrong with my query and how can i resolve it ?? Requesting help..
Try this query:
SELECT t1.`date`,
t1.`code`,
t3.`followup_status`
FROM `daily_data_update` AS t1
LEFT JOIN followupstatus AS t3 ON t1.followup_status = t3.id
LEFT JOIN users AS t2 ON t1.user_id = t2.id
WHERE t2.id IS NOT NULL
AND t2.user_region = 'scotland'
Hope this works!!
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 help getting the nth number of a result in MySQL.
I have three tables with the following fields:
table1: product_id, name, description
table2: category_id, name, description
table3: product_id, category_id
I'm trying to get this result:
name | description - product_category -category2 - category3
___________________________________________________________________
NAME 1 | Description 1 | Cat1 | Cat2 | Cat5
NAME 2 | Description 2 | Cat7 | Cat9 | Cat11
The query I'm trying to use is as follows:
SELECT
t1.name AS product_name,
t1.description AS product_description,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 1) AS product_category,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 2) AS product_category2,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 3) AS product_category3
FROM table1 t1 INNER JOIN
table3 t3
ON t3.product_id = t1.product_id INNER JOIN
table2 t2
ON t2.category_id = t3.category_id
;
And sadly I get this result:
name - description - category
NAME 1 - Description 1 - Category 1
NAME 2 - Description 2 - Category 2
Try using LEFT JOIN instead of INNER JOIN.
Example:
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2
How can i edit the ON operator part of my query below such that i would like the current code to work where id<=14 (which is t2.id <= t1.id as shown below) so when t1 id =14, t2 is the cumulative id from 1 to 14 (as it is now).
but for id >14 I would like the ON operator to be (t2.id=t1.id>=t1.id-2 and <=t1.id) so when t1 id=15, t2.id should be between 13 and 15. when t1 id =16, t2.id should be between 14 and 16 and so on.
I'm doing this because when i calculate col E for ids after id=14, i am only interested in getting the average of the previous 2 rows for C and D on a moving average.
My query has 2 sub queries and it updates column E. The table looks like this:
--------------------------
id | A | B | E |
--------------------------
1 | NULL | NULL |NULL|
--------------------------
2 | 4 | 6 |NULL|
--------------------------
3 | 6 | 9 |NULL|
--------------------------
This is my query where i got help from this link: Mysql Nested sub queries unknown column error
Update t join
(SELECT t1.id ,ifnull(t1.A/AVG(t2.A),0) C ,ifnull(t1.B/AVG(t2.B),0) D
FROM t t1
JOIN t t2
ON t2.id <= t1.id
group by t1.id ) AS tt
on(t.id = tt.id)
SET E = (tt.C + tt.D)/2;
Thanks,
where id<=14 (which is t2.id <= t1.id as shown below) so when t1 id =14, t2 is the cumulative id from 1 to 14 (as it is now).
Update t join
(
SELECT t1.id ,ifnull(t1.A/AVG(t2.A),0) C ,ifnull(t1.B/AVG(t2.B),0) D
FROM t t1
JOIN t t2
ON case when t2.id < 15 then t2.id <= t1.id else t2.id=t1.id>=t1.id-2 and <=t1.id end
group by t1.id
) tt on(t.id = tt.id)
SET E = (tt.C + tt.D)/2;
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.
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);