Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL query, joining 2 table's issue

I have this query -

SELECT interest_desc, categoryID, MAX(num_in_cat) AS num_in_cat 
FROM
(
   SELECT interest_desc, categoryID, COUNT(categoryID) AS num_in_cat
   FROM interests
   GROUP BY interest_desc, categoryID
 ) subsel 
 GROUP BY interest_desc, categoryID

I want to change it so that I can eventually display the category name from a separate table called categories. All I can display is the categoryID from interests with this sql

Both table structures are

#interests

CREATE TABLE `interests` (
 `interestID` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(100) NOT NULL,
 `categoryID` int(11) NOT NULL,
 `sessionID` int(11) NOT NULL,
 `interest_desc` varchar(30) NOT NULL,
 `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`interestID`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8   

categories table structure

# categories
CREATE TABLE `categories` (
 `categoryID` int(11) NOT NULL AUTO_INCREMENT,
 `category_desc` varchar(100) NOT NULL,
 PRIMARY KEY (`categoryID`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

I know a join of some sort is needed but I have looked at examples and are struggling to get the exact syntax.

I have this in a php script - the echo statement is this

"{$result['interest_desc']} was the most popular in category   {$result['categoryID']}    with {$result['num_in_cat']} occurrences\n";

and its output is this -

"Adidas was the most popular in category 5 with 1 occurrences"

I want the output to be "Adidas was the most popular in Sport with 1 occurrences"

However my sql query does not feature category_desc.

like image 507
DIM3NSION Avatar asked May 03 '12 06:05

DIM3NSION


People also ask

Can we join 2 tables without on condition?

Yes, you can! The longer answer is yes, there are a few ways to combine two tables without a common column, including CROSS JOIN (Cartesian product) and UNION. The latter is technically not a join but can be handy for merging tables in SQL.

Can we join more than 2 tables in MySQL?

It is possible to use multiple join statements together to join more than one table at the same time. To do that you add a second INNER JOIN statement and a second ON statement to indicate the third table and the second relationship.

How do I merge two MySQL queries?

The MySQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION operator must have the same number of fields in the result sets with similar data types.

Can we join two tables different databases in MySQL?

MySQL Joins let you access data from multiple tables. A MySQL Join is performed whenever two or more tables are joined in an SQL statement.


2 Answers

This is more quick performance wise

SELECT subsel.interest_desc, subsel.categoryID, cat.category_desc, MAX(num_in_cat) AS num_in_cat 
    FROM
    (
       SELECT interest_desc, categoryID, COUNT(categoryID) AS num_in_cat
       FROM interests
       GROUP BY interest_desc, categoryID
     ) subsel 
     inner join categories as cat on subsel.categoryID = cat.categoryID
     GROUP BY interest_desc, subsel.categoryID
like image 147
Maulik Vora Avatar answered Oct 16 '22 16:10

Maulik Vora


Kindly check this , It will give you the required result.

SELECT subsel.interest_desc, cat.category_desc, MAX(num_in_cat) AS num_in_cat 
FROM
(
   SELECT interest_desc, categoryID, COUNT(categoryID) AS num_in_cat
   FROM interests
   GROUP BY interest_desc, categoryID
 ) subsel 
 inner join categories as cat on subsel.categoryID = cat.categoryID
 GROUP BY interest_desc, subsel.categoryID
like image 30
Shaikh Farooque Avatar answered Oct 16 '22 14:10

Shaikh Farooque