MYSQL Order by sum of different table - mysql

I have two tables:
cat_seriale - which represents the serial categories and provides ID's for each category like: 1, 2, 3, 4, 5.
seriale - which is actual tv serials, and each tv serial falls in one category.
I am trying to
SELECT * FROM cat_seriale WHERE `id`='1'
and additionally to all columns, display the sum of views column from all rows in seriale table.
If some one can help me out, that would be great.
Thanks in advance.
SCHEMA:
cat_seriale columns:
Primary Key - catid(int)
catname (varchar)
...
seriale columns:
Primary Key - id(int)
cat (int)
views(int)
I need to select cat_seriale where ID = 1, and select sum of views in seriale columns where cat is same with id from cat_seriale.

Like this:
select *, (select sum(views) from seriale S where S.cat=C.catid) as sum_views
from cat_seriale C
where id='1'
order by sum_views

Join the two tables.
SELECT c.*, SUM(s.views) AS views
FROM cat_seriale AS c
LEFT JOIN seriale AS s ON c.catid = s.cat
WHERE c.id = `1`
GROUP BY c.catid

+1 for #Mike answer, but this version has more performance:
SELECT c.*, SUM(s.views) as summary
FROM cat_seriale c
LEFT JOIN seriale s ON s.cat = c.catid
WHERE c.id = '1'
GROUP BY c.catid
ORDER BY summary

Related

how to get Intersection of two query in mysql

I have a table that has two column with two foreign key from two different table.
this is my relation table:
I want to select those student who can speak both language with id 3 and 4.
How can i wrote a query to give me for e.x 12 , 14
You can give it a try:
SELECT
student_id,
COUNT(*) total
FROM your_table
WHERE language_id IN (3,4)
GROUP BY student_id
HAVING COUNT(*) = 2;
Only IN doesn't ensure that a student is involved both in language id 3 & 4.
You need to use GROUP BY student_id HAVING COUNT(*) = 2 in order to ensure those student_ids to be in the result who were involved both in language id 3 & 4
Another solution would be using INNER JOIN. But it doesn't scale.
SELECT
A.student_id
FROM your_table A
INNER JOIN your_table B ON A.student_id = B.student_id
AND A.language_id = 3 AND B.language_id = 4
Assume your relation is named "my-relation":
SELECT R1.student_Id FROM my-Relation R1 join my-Relation R2 on R1.student_id = R2.student_id where R1.language_Id = '3' and R2.language_id = '4'

Concat foreign key values from a self related table

I have a products database which has a multi-tier category structure. Products are assigned to a category. The category table looks like this:
id name parent_id
================================
1 Electronics NULL
2 AV 1
3 Speakers 2
4 Wireless 3
What I want to do is, as part of my SELECT statement for products, output a concatenated string of the category tree.
The product is always assigned to the last category, so for example, Product "500w Wireless Speakers" would be assigned to category_id 4 (based on the above).
The ouputted column should be Electronics-AV-Speakers-Wireless.
Is this possible to do? I have looked at GROUP_CONCAT() but I'm having trouble working out the correct syntax.
Join as many times as you need, and concat the names:
select concat(a.name, '-', b.name, '-', c.name, '-', d.name) name
from mytable a
join mytable b on a.id = b.parent_id
join mytable c on b.id = c.parent_id
join mytable d on c.id = d.parent_id;

What is the wrong with this query?

I have 3 sql tables calls category, movies, category_movies. category and movies tables have many to many relationship. Thats why I use category_movies table. This is tables' structure...
Category : cat_id, cat_name,
movies : mov_id, mov_name,
category_movies : cat_id, mov_id
Now I have got 3 category IDs dynamically and now I want to select movies' names alone with category names which belongs to 3 category_id have already got.
This is the query that I tried so far..
SELECT c.cat_name AS cn, m.mov_name AS mn, m.mov_id
FROM category AS c
INNER JOIN category_movies AS cm ON cm.cat_id = c.cat_id
INNER JOIN movies AS m ON m.mov_id = cs.mov_id
WHERE c.cat_id IN (2, 5, 7)
GROUP BY c.cat_name, m.mov_name, m.mov_id
HAVING COUNT(*) >= 3
but this is now working.. can anybody tell me what is wrong with this query?
use IN clause on this
SELECT..
FROM..
WHERE cat_id IN (2, 5, 7)
and is the same with
SELECT..
FROM..
WHERE cat_id = 2 OR
cat_id = 5 OR
cat_id = 7
Please also take note that it is INNER JOIN not INNOR JOIN
but I guess, you want to perform RELATIONAL Division (you want to search for a movie that has all the category you want to find)
SELECT c.cat_name, m.mov_name, m.mov_id
FROM category AS c
INNER JOIN movies AS m ON m.cat_id = c.cat_id
INNER JOIN category_movies AS cm ON cm.mov_id = m.mov_id
WHERE cat_id IN (2, 5, 7)
GROUP BY c.cat_name, m.mov_name, m.mov_id
HAVING COUNT(*) >= 3
Relational Division
INNOR -> INNER
WHERE cat_id = 2 AND 5 AND 7
that hardly can be right it probably should be OR
1 code
SELECT Id, name, YEAR(BillingYar) AS Year
FROM Records
WHERE Year ≥ 2010
2 code
SELECT id, name
FROM students
WHERE grades = (SELECT MAX(grades)
FROM students
GROUP BY subject_id);
I can see only in first code wrong this symbol ( ≥ ), need to be >= . Something else?

A Better way to optimize these MySQL queries

I have two MySQL tables:
attributes (attributeid, name)
productsattributes (productid, attributeid, displayvalue)
The required is for each attribute name called "Product Type" get all other attributes associated with this "Product Type". As an example — attributes table will look like:
attributeid name
1 A
2 B
3 Product Type
4 D
productsattributes table will look like:
productid attributeid displayvalue
1 3 FAN
1 1 Brown
1 2 Stand
2 3 FAN
2 4 D
3 3 CAR
3 4 imported
So the final result should be:
FAN (A,B, Product Type,D)
CAR (Product Type, imported)
Here is my try:
first I get all the "displayvalues" from productattributes:
SELECT DISTINCT displayvalue
FROM productsttributes
WHERE attributeid = 3;
then I loop through each "displayvalues" to find the other attributes:
SELECT a.name
FROM attributes a
INNER JOIN productsattributes pa
ON pa.attributeid = a.attributeid AND productid in (
SELECT productid
FROM productsttributes
WHERE dispalyvale = '$displayvalue')
ORDER BY a.name;
The problem is the productattributes table has about 7 million rows, so my script is taking forever .. of course I am not looking for 10 minutes solution but at least it will improve my queries a bit.
I would start with the following statements:
ALTER TABLE attributes ADD CONSTRAINT p_attributes PRIMARY KEY (attributeid);
ALTER TABLE productsattributes ADD CONSTRAINT p_productsattributes
PRIMARY KEY(productid, attributeid);
ANALYZE TABLE attributes, productsattributes;
This will make sure all important fields are indexed.
The query might look like this (also on SQL Fiddle):
SELECT trg.displayvalue,
group_concat(a.name ORDER BY trg.productid, a.attributeid)
FROM (
SELECT t.productid,t.displayvalue
FROM attributes a
JOIN productsattributes t USING (attributeid)
WHERE a.name = 'Product Type') AS trg
JOIN productsattributes p ON p.productid = trg.productid
JOIN attributes a ON a.attributeid = p.attributeid
GROUP BY trg.displayvalue
ORDER BY 1;
Please, kindly include the EXPLAIN output of your's and this queries into your question.
Try this ::
Select displayvalue, attribute_name
from
(Select
product_id from productsattributes pa inner join attributes_table at on (pa.attributeid=at.id) where at.name=?) as productList
inner join productsattributes pa2 on(pa2.product_id=productList.product_id)
inner join attributes_table at2 on (pa2.attributeid=at2.id)

selecting from multiple tables with counter

I have two tables one holds a category with column catID,catName and the other has the catID as a foreign key, now i want to select all the total items on the second table based on their catID.
E.g. What is the total number of individual element if their category is 1,2,3,4 etc. Pls code hints will help thanks.
SELECT
c.cat_id
,count(*) as occurence
FROM category c
INNER JOIN table2 t ON (c.cat_id = t.cat_id)
GROUP BY c.cat_id
If you want the categories with occurence = 0 then do:
SELECT
c.cat_id
,count(t.cat_id) as occurence
FROM category c
LEFT JOIN table2 t ON (c.cat_id = t.cat_id)
GROUP BY c.cat_id
Links:
http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html
http://www.1keydata.com/sql/sqlgroupby.html
SELECT catName, COUNT(table2.catId) FROM table1,table2
WHERE table1.catId=table2.catId
GROUP BY catName