Hi I have two questions about MySQL and QUERIES.
First of all i've like to know what diffrence does it make at forming queries if i setup relationships between the tables in phpmyadmins designer or if i dont?
And second I have 3 tables and have some logical error in my query it seems that MAX() function and GROUP BY do not go well together ://
So if anyone know how to solve my problem, please share :)
And yes I'm using MySQL 5.1.41 and I made the relationships between tables
CREATE TABLE `sandbox`.`videos` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 255 ) NOT NULL ,
`views` INT NOT NULL ,
`id_user` INT UNSIGNED NOT NULL ,
`id_type` INT UNSIGNED NOT NULL ,
INDEX ( `id_user` , `id_type` )
) ENGINE = INNODB;
id title views id_user id_type
------------------------------------
1 video1 50 1 1
2 video2 55 3 1
3 video3 100 2 3
4 video4 20 5 3
5 video5 62 4 5
CREATE TABLE `sandbox`.`users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 50 ) NOT NULL
) ENGINE = INNODB;
id name
-----------
1 adam
2 mike
3 chuck
4 walker
5 nancy
CREATE TABLE `sandbox`.`types` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 50 ) NOT NULL
) ENGINE = INNODB;
id name
-------------
1 20 min
2 30 min
3 50 min
4 90 min
5 120 min
MY QUERY:
SELECT videos.id, videos.title, MAX(videos.views) AS views, users.name AS user, types.name AS type
FROM videos, users, types
WHERE videos.id_user = users.id
AND videos.id_type = types.id
GROUP BY id_type
ORDER BY id_type ASC
LIMIT 0 , 20;
QUERY RESULT:
id title views user type
------------------------------------
1 video1 55 adam 20 min
3 video3 100 mike 50 min
5 video5 62 walker 120 min
The GROUP BY function groups all entries by type and takes data only from first entry in the group, but because of MAX() function it copies the highest value from views and writes it to the first entry in the group.
WANTED RESULT:
id title views user type
------------------------------------
1 video2 55 chuck 20 min
3 video3 100 mike 50 min
5 video5 62 walker 120 min
What I need is that it selects all the data (title and username also) not only views data and groups them by type.
Thx for any help :)