sql - How to simplify embedded mysql query into a JOIN? -
sql - How to simplify embedded mysql query into a JOIN? -
i read performance problems embedded mysql queries, wanted know how alter next "join" (supposedly improve performance?).
i have 2 tables:
create table if not exists `blog_categories` ( `category_id` int(11) not null auto_increment, `category_name` varchar(300) collate utf8_unicode_ci not null, `category_name_url` varchar(300) collate utf8_unicode_ci not null, `category_status` enum('online','offline') collate utf8_unicode_ci not null default 'offline' ) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=8 ; create table if not exists `blog_articles` ( `article_id` int(11) not null auto_increment, `article_title` tinytext collate utf8_unicode_ci not null, `category_name` varchar(100) collate utf8_unicode_ci not null ) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=26 ; the logic select categories have articles associated them. each row in blog_articles table includes category_name , here query i'm using (which checks out , works fine):
$sql = "select category_name , category_name_url blog_categories ( (select count(*) blog_articles, blog_categories blog_articles.category_name = blog_categories.category_name ) > 0 , blog_categories.category_status = 'online')"; i'm still new "join", , not sure how alter when using "count(*)" in mix too.
rather count(*) > 0, utilize exists
"select category_name, category_name_url blog_categories exists (" . "(select 1 blog_articles inner bring together blog_categories on blog_articles.category_name = blog_categories.category_name) , " . "blog_categories.category_status = 'online'" . ")"; this old style bring together syntax:
select 1 blog_articles, blog_categories blog_articles.category_name = blog_categories.category_name this ansi form of same:
select 1 blog_articles inner bring together blog_categories on blog_articles.category_name = blog_categories.category_name update (in response posters comments): query require:
"select category_name, category_name_url blog_categories category_name in (select distinct category_name blog_articles) , blog_categories.category_status = 'online'" mysql sql performance join
Comments
Post a Comment