MySQL sum + inner join query - mysql

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

Related

Join query results in MySQL

I am working in a MySQL database.
I have three tables. 1 , 2 & 3
I would like to join the result of joining tables 2 & 3 to table 1 on an id. I would like to keep all entries for table 1 and after join the result of 2 & 3 on the call id and where it doesn't match have a null value.
Table 1 has callid
Table 2 has callid and invoiceid
Table 3 has invoiceid and customerid
So join table 2 & 3 on invoiceid and filter by customerid = xyz the result of that is then joined to table 1 on callid. Table 1 would also have a Where clause filtering the on a date
result would look like this
callid customerid
123 xyz
124 xyz
125 null
126 xyz
thanking you in advance
I think you are describing left join:
select . . .
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid;
You can filter on customer id in the on clause:
from table1 t1 left join
table2 t2
on t1.callid = t2.callid left join
table3 t3
on t3.invoiceid = t2.invoiceid and t3.customerid = ?;
However, this might not be exactly what you want. You might want to filter for the inner join and then do the left join:
select . . .
from table1 t1 left join
(table2 t2 join
table3 t3
on t3.invoiceid = t2.invoiceid
)
on t1.callid = t2.callid and
t3.customerid = ?

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 |

mysql select from 3 tables where not all tables contain infos in the same amount of rows (Inner Join?!)

Looks like I am not able to understand some of the solutions given here and in other places, so I decided to ask my own question.
I have three tables. Like this:
table1
id name active
123 item1 1
234 item2 0
345 item3 1 <--- not in table2!!
456 item4 1
567 item5 1
table2
id item_id instock variants deliverytimes
1 123 0 S 21days
2 123 1 M 21days
3 123 2 L 21days
4 456 1 white 10days
5 456 0 black 10days
6 234 0 yellow sold
7 456 1 green sold
8 456 0 red sold
9 567 0 big sold
table3
id item_id description
1 123 Cool Shirt
2 234 Collectors Box
3 345 Comicbook
4 456 Basecap OneSize
5 567 Cool-Mug
I tried several attempts from LEFT JOIN to RIGHT JOIN etc. that all ended up with multiple results that are not DISTINCT in the ID of the first table that I need nor they were complete. Means that if table2 does not comntain item of table1 it won't show in the result.
The closest I got is this:
select * from table1 t1
INNER JOIN (
SELECT * FROM table2
where (deliverytimes!='sold' OR instock>0) LIMIT 1 ) t2
ON t1.id=t2.item_id,
table3 t3
where t1.active = '1'
and t1.id = t2.item_id and t3.item_id = t1.id;
In the end I need a list of:
"id, name, description"
result (as I would like it to be)
id name description
123 item1 Cool Shirt
345 item3 Comicbook
456 item4 Basecap OneSize
that does need to meet this requirements:
"item needs to be t1.active=1"
and
"if items has rows in t2 show only if one row equals (instock>0 or deliverytimes!=sold)"
and
"if item has no rows in t2 show as long as t1.active=1"
Last one is the problem. I never get distinct t1.id when I use other than inner join and with inner join I still miss the rows that are not present in t2 but are still active=1.
When starting to write a query think about what data you want to show. In your case you want to show data from table1 and table3, so join these and select from them. The criteria on table2 belong in the WHERE clause.
The criteria on table2 are:
either no entry in t2 exists
or an entry in t2 exists with instock > 0 or deliverytimes != sold
This means one EXISTS clause, one NOT EXISTS clause, both combined with OR.
select t1.id, t1.name, t3.description
from t1
join t3 on t3.item_id = t1.item_id
where t1.active = 1
and
(
not exists
(
select *
from t2
where t2.item_id = t1.item_id
)
or
exists
(
select *
from t2
where t2.item_id = t1.item_id
and (instock > 0 or deliverytimes != 'sold')
)
);
try this
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
if you want to filter you result with active = 1 and != soled filter in where clause
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1 AND t2.deliverytimes != 'sold'
OR
SELECT DISTINCT(t1.id) as ID, t1.name, t3.description
FROM table1 AS t1
JOIN table2 AS t2 ON t2.item_id = t1.id AND t2.deliverytimes != 'sold'
LEFT JOIN table3 AS t3 ON t1.id = t3.item_id
WHERE t1.active = 1
You can try to link table1 with table2 using Left outer join (this will make sure you will get all records from table1). And now link tbale3 as inner join, this will make sure to get all records which are related with table1.
Table1 LEFT OUTER JOIN TABLE2 and Table1 INNER JOIN table3.
Above is my assumption, you can try the above logic.

MySQL trouble getting value from a third table in query

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*/

Query 2 MYSQL tables + condition?

Format -> column.example_data
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.
TRY
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
Try with something like:
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.t1_id = t1.id
INNER JOIN table3 t3 ON t3.t2_id = t2.id
Where t1_id and t2_id are the fields that are referring the records in the parent tables. I recommend you to add an index on those fields also.