MySQL trouble getting value from a third table in query - mysql

I need help retrieving a value from a third table
product_attribute
| id_product_attribute | id_product | reference | ean13 |
product_attribute_combination
| id_attribute | id_product_attribute |
attribute_lang
| id_attribute | name |
I did the query below to get the product list, but I also want to get the name from attribute_lang and I don't know how.
Can anyone help me on this?
This is what I have now
SELECT T1.id_product, T1.reference, T2.name, T1.price, IF(LENGTH(TRIM(T1.ean13)) = 0, T1.id_product, T1.ean13) AS ean13
FROM /*PREFIX*/product_attribute T1
INNER JOIN /*PREFIX*/product_lang T2 ON (T1.id_product = T2.id_product AND T2.id_lang = /*CURRENT_LANGUAGE_ID*/)
WHERE T1.id_product /*PRODUCTS_ID_LIST*/

you will need to add 2 more joins:
LEFT JOIN /*PREFIX*/product_attribute_combination T3 ON T3.id_product_attribute=T1.id_product_attribute
LEFT JOIN /*PREFIX*/attribute_lang T4 ON T4.id_attribute=T3.id_attribute
and add the T4.name as a selected column
The result will be something like this:
SELECT T1.id_product, T1.reference, T2.name, T1.price, IF(LENGTH(TRIM(T1.ean13)) = 0, T1.id_product, T1.ean13) AS ean13, T4.name
FROM /*PREFIX*/product_attribute T1
INNER JOIN /*PREFIX*/product_lang T2 ON (T1.id_product = T2.id_product AND T2.id_lang = /*CURRENT_LANGUAGE_ID*/)
LEFT JOIN /*PREFIX*/product_attribute_combination T3 ON T3.id_product_attribute=T1.id_product_attribute
LEFT JOIN /*PREFIX*/attribute_lang T4 ON T4.id_attribute=T3.id_attribute
WHERE T1.id_product /*PRODUCTS_ID_LIST*/

Related

MySQL: Join three tables, comparing two values

First of all, I'm an amateur on SQL. Here it is the example. From this three tables I would like to know who are the teachers that make more money than Mike
Table1:
LessonName TeacherID
Maths 3
Biology 2
Biology 4
Geology 1
Table2:
Lesson PricePerClass
Maths 200
Biology 100
Geology 150
Table3:
IDTeacher TeacherName
1 Mike
2 John
3 Lauren
4 Julian
So far I've made this:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
where TotalRevenue > (select TotalRevenue
from Table2 as t2
inner join Table1 as t1 on t2.Lesson = t3.LessonName
inner join Table3 as t3 on t3.IDTeacher = t1.TeacherID
where t3.TeacherName = "Mike");
And I don't know how to keep going because when I run it, an error appears.
My expected result would be something like:
IDTeacher TotalRevenue
3 200
Thanks!
In your main query you must use a HAVING clause instead of a WHERE clause and also in the subquery fix your joins:
select t3.IDTeacher, sum(t2.PricePerClass) TotalRevenue
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
group by t3.IDTeacher
having TotalRevenue > (
select sum(t2.PricePerClass)
from Table3 t3
inner join Table1 as t1 on t1.TeacherId = t3.IDTeacher
inner join Table2 as t2 on t2.Lesson = t1.LessonName
where t3.TeacherName = "Mike"
);
See the demo.
Results:
| IDTeacher | TotalRevenue |
| --------- | ------------ |
| 3 | 200 |

SQL query to get Freinds of Freinds of Freinds (depth 3)

I have 2 tables namely t_users, and t_user_freinds. The Schema is quite simple:
-------- --------
|t_users| | t_users_freinds|
-------- -----------------
| id | | id |
-------- ------------------
| name | |user_1 |
-------- ----------------
| user_2 |
----------------
My question is how do I get friends of friends of friends (depth level 3) for a given person with id = 1?
An id is just a number from 1 to x. The user_1 is a friend of user_2. Both user_1 and user_2 exist in the t_user table.
I'm fairly new to SQL.
Thanks.
UPDATE:
I tried this
select t1.id, t1.name,
t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t2.user_1 = t2.user_2
WHERE t1.id = "1"
But it did not work and gives a result of null (no errors though.)
For finding only the friends of a user:
select t1.id, t1.name,
t2.user_2 as friends
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
WHERE t1.id = "1"
This worked as expected.
Finding Freinds of Freinds of Freinds
select t1.id, t1.name, t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t3.user_1 = t2.user_2
WHERE t1.id = "5"
This SQL query seems to work as expected. Thanks everyone.
This SQL query seems work as expected and answers my question:
select t1.id, t1.name, t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t3.user_1 = t2.user_2
WHERE t1.id = "5"
Thanks, everyone.
If i understand your datamodel correctly try below,
SELECT users.id,
users.name,
frnds.user_2 as friends,
frndsoffriends.user_2 as friendsoffriends
FROM t_user_friends frnds,
t_user_friends frndsoffriends,
t_users users
WHERE users.id = 1
AND frnds.user_1 = users.id
AND frnds.user_2 = frndsoffriends.user_1
EDIT: With explicit JOIN in the statement if that is your preferred coding style.
select t1.id, t1.name,
t2.user_2 as freinds, t3.user_2 as freinds_of_freinds
from t_user t1
inner join t_user_friend t2 on t2.user_1 = t1.id
inner join t_user_friend t3 on t2.user_2 = t3.user_1
WHERE t1.id = 1

JOIN OR SELECT IN SELECT [MYSQL]

please help me to join these two selects:
SELECT IFNULL(sum(estimated_hours * t2.man_hour),0) as
estimated from project_has_tasks t1 left join users t2
on t1.user_id = t2.id group by project_id
SELECT IFNULL(sum(TIME_FORMAT(SEC_TO_TIME
(time_spent),'%k.%i' )* t2.man_hour),0) as time_spent_cost FROM project_has_tasks t1
left join users t2 on t1.user_id = t2.id group
by project_id
Wanna to get:
| estimated | time_spent_cost |
_______________________________
| 000000000 | 00000000 |
Simply put them in one query:
SELECT IFNULL(sum(estimated_hours * t2.man_hour),0) as
estimated, IFNULL(sum(TIME_FORMAT(SEC_TO_TIME
(time_spent),'%k.%i' )* t2.man_hour),0) as time_spent_cost
from project_has_tasks t1 left join users t2
on t1.user_id = t2.id group by project_id

combining multiple columns in two tables

Here is my tables:
T1 record:
id referencePerson1ID referencePerson2ID referencePerson3ID
1 1 2 3
T2 referencePerson:
id name
1 Peter
2 John
3 Mary
I want to get the following result when I choose T1 id =1:
id referencePerson1 referencePerson2 referencePerson3
1 Peter John Mary
How can I do that?
Thanks
This is one basic way to do things:
SELECT T1.id, P1.name, P2.name, p3.name
FROM record T1 LEFT JOIN referencePerson P1
ON T1.referencePerson1ID=P1.id
LEFT JOIN referencePerson P2
ON T1.referencePerson2ID=P2.id
LEFT JOIN referencePerson P3
ON T1.referencePerson3ID=P3.id
Another way when number of persons is unknown can be done using a PIVOT
You can do that with three times JOIN :
SELECT t1.id, t21.name, t22.name, t23.name
FROM T1
INNER JOIN T2 t21 ON t21.id = T1.referencePerson1ID
INNER JOIN T2 t22 ON t22.id = T1.referencePerson2ID
INNER JOIN T2 t23 ON t23.id = T1.referencePerson3ID
WHERE T1.id = 1

MySQL sum + inner join query

Question:
Table 1: id.1 | name.Joe | etc.Some | ...Other | ...Data
Table 2: id.X | number.+1 123 555 9999 | useridfromtable1.1 -> Linking telefone with Joe
Table 3: id. X | number.+1 123 555 9999 | calls.55
I need a query that join the 3 tables and I only have the id (userid) from the table 1.
So, I need from userid -> grab the telephone -> from the telefone grab calls on table3.
Answer:
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
New Question?
Any chance you know how can I print the sum of calls on the result? after all these join I get about 10 lines of the same user and their respective calls based on the phone, what is correct for what I asked, now I need return all the calls in 1 line with the values:
sum | user ID | user Name
Maybe this will be useful:
SELECT SUM(t3.calls) FROM table1 t1 INNER JOIN table2 t2 ON t2.useridfromtable=t1.id INNER JOIN table3 t3 ON t3.number = t2.number
Try this query:
SELECT sum(t3.calls), t1.id, t1.name
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
GROUP BY t1.id