need help in writing complex sql query with join and with case - mysql

Here is SQL Fiddle
Expected output is below:
id | firstname. | lastname. | country.| followerscount.| followingcount.| BottonText
------------------------------------------------------------------------------------
54 prince lal nepal 1 1 Unfollow
56. Rekha. Gupta United States 1 0 Unfollow
57. Nirmal null null 0 1 Follow Back
I tried below query assuming current logged in UserId= 55 & compare with other users. Can you write the correct query, it can be with join, i just need the output,query can be anything
TRY1
SELECT
u.user_id AS id, u.first_name AS firstname,u.last_name AS lastname,u.country as country,
(select count(uf.following_user_id) from user_follow uf where uf.followed_user_id=u.user_id)as followerscount,
(select count(uf.followed_user_id) from user_follow uf where uf.following_user_id=u.user_id)as followingcount,
CASE
WHEN uuf.following_user_id=55 AND uuf.followed_user_id=u.user_id THEN "Unfollow" # A is following B == unfollow
WHEN uuf.following_user_id=u.user_id AND uuf.followed_user_id NOT IN(55) THEN "Follow Back" #B is following A but A is not following B = followBack
ELSE "Follow"
END AS BottonText
FROM userdetails u,user_follow uuf
WHERE u.user_id NOT IN(55) AND u.record_status=1
TRY2
SELECT
u.user_id,
(count(uf.following_user_id)) as followers,
CASE
WHEN uf.following_user_id=55 AND uf.followed_user_id=u.user_id THEN "Unfollow" # A is following B == unfollow
WHEN uf.following_user_id=u.user_id AND uf.followed_user_id=55 AND uf.following_user_id !=55 AND uf.followed_user_id !=u.user_id
THEN "Follow Back" #B is following A but A is not following B = followBack
ELSE "Follow"
END AS BottonText,
(select count(uf.followed_user_id)
from user_follow uf
where uf.following_user_id = u.user_id) as following
from
(userdetails u left outer join user_follow uf on uf.followed_user_id=u.user_id)
where u.record_status=1 and u.user_id NOT IN(55)
group by u.user_id
Table defination
CREATE TABLE `userdetails` (
`user_id` int(11) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) DEFAULT NULL,
`country` varchar(30) DEFAULT NULL,
`avatar_img_name` varchar(30) DEFAULT NULL,
`record_status` int(1) NOT NULL COMMENT '1- active'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `userdetails`
ADD PRIMARY KEY (`user_id`);
CREATE TABLE `user_follow` (
`id` int(11) NOT NULL,
`following_user_id` int(11) NOT NULL,
`followed_user_id` int(11) NOT NULL,
`created_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
#following_user_id and followed_user_id are foriegn key associated userdetails user_id

http://sqlfiddle.com/#!9/90d4ac/18
schema:
CREATE TABLE `userdetails` (
`user_id` int(11) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) DEFAULT NULL,
`country` varchar(30) DEFAULT NULL,
`avatar_img_name` varchar(30) DEFAULT NULL,
`record_status` int(1) NOT NULL COMMENT '1- active'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `userdetails`
ADD PRIMARY KEY (`user_id`);
INSERT INTO `userdetails` (`user_id`, `first_name`, `last_name`, `country`, `avatar_img_name`, `record_status`) VALUES
(1, 'Admin', 'Admin', 'India', NULL, 0),
(54, 'Prince', 'Lal', 'Nepal', 'p.jpg', 1),
(55, 'ashutosh', 'Singh', 'Afganistan', 'a.jpg', 1),
(56, 'Rekha', 'Gupta', 'United States', 'r.jpg', 1),
(57, 'Nirmal', NULL, NULL, NULL, 1);
CREATE TABLE `user_follow` (
`id` int(11) NOT NULL,
`following_user_id` int(11) NOT NULL,
`followed_user_id` int(11) NOT NULL,
`created_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `user_follow` (`id`, `following_user_id`, `followed_user_id`, `created_date`) VALUES
(1, 55, 56, '2021-02-07 02:14:26'),
(2, 55, 54, '2021-02-06 01:14:26'),
(3, 54, 55, '2021-02-07 10:33:45'),
(4, 57, 55, '2021-02-07 00:00:00');
ALTER TABLE `user_follow`
ADD PRIMARY KEY (`id`),
ADD KEY `following_user_id` (`following_user_id`),
ADD KEY `followed_user_id` (`followed_user_id`);
ALTER TABLE `user_follow`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
ALTER TABLE `user_follow`
ADD CONSTRAINT `user_follow_ibfk_1` FOREIGN KEY (`followed_user_id`) REFERENCES `userdetails` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `user_follow_ibfk_2` FOREIGN KEY (`following_user_id`) REFERENCES `userdetails` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
query:
SELECT
u.user_id, u.first_name, u.last_name, u.country,
(SELECT COUNT(*) FROM user_follow uf1 WHERE uf1.followed_user_id=u.user_id) AS followerscount,
(SELECT COUNT(*) FROM user_follow uf2 WHERE uf2.following_user_id=u.user_id) AS followingcount,
IF(
EXISTS(SELECT * FROM user_follow uf3 -- if I'm following the specified user
WHERE uf3.following_user_id=55 AND uf3.followed_user_id=u.user_id),
'Unfollow', -- then show 'unfollow button'
IF(
EXISTS(SELECT * FROM user_follow uf4 -- if the specified user is following me
WHERE uf4.followed_user_id=55 AND uf4.following_user_id=u.user_id),
'Follow Back', -- then show 'follow back button'
'Follow') -- then show 'follow button'
) AS ButtonText
FROM
userdetails u
WHERE
u.user_id<>55
AND u.user_id<>1 -- should not select the admin user
explanation: it seems that the expected table is based on "userdetails" table, so you should only select "userdetails" in FROM clause, and add other column with subquery in SELECT clause

Related

SQL query not returning correct result using three table together?

I have 3 tables in which tbl_user_signup_info, tbl_main_lead_info and tbl_campaign_info, I need a result in which it display info such that each row display the lead id and campaign id added by corresponding registered users of the tbl_user_signup_info and display should be like User_Id, Lead_Id, Campaign_ID.
Actually i want Total number of leads added by particular user with there Lead Id and Total number of campaign added by that user with there Campaign_Id using those 3 tables.
But i am lacking to form SQL query.
My Result are as below which are wrong:
Table structure
--
-- Table structure for table `tbl_campaign_info`
--
DROP TABLE IF EXISTS `tbl_campaign_info`;
CREATE TABLE IF NOT EXISTS `tbl_campaign_info` (
`Campaign_Id` int(100) NOT NULL AUTO_INCREMENT,
`CampaignName` varchar(200) NOT NULL,
`CampaignStatus` varchar(200) NOT NULL,
`CampaignDescription` varchar(200) DEFAULT NULL,
`CampaignOwnerNotes` varchar(200) DEFAULT NULL,
`CampaignAdminNotes` varchar(200) DEFAULT NULL,
`CampaignStartDate` date NOT NULL,
`CampaignEndDate` date NOT NULL,
`CampaignLead_Id` int(100) NOT NULL,
`CampaignAddedBy` int(100) NOT NULL,
`CampaignAddedOn` date DEFAULT NULL,
UNIQUE KEY `Campaign_Id` (`Campaign_Id`)
) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;
--
-- Table structure for table `tbl_main_lead_info`
--
DROP TABLE IF EXISTS `tbl_main_lead_info`;
CREATE TABLE IF NOT EXISTS `tbl_main_lead_info` (
`Lead_Id` int(100) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(100) DEFAULT NULL,
`LastName` varchar(100) DEFAULT NULL,
`Company` varchar(100) DEFAULT 'NA',
`Website` varchar(100) DEFAULT 'NA',
`Designation` varchar(100) DEFAULT 'NA',
`Linkedin` varchar(100) DEFAULT 'NA',
`Email` varchar(100) DEFAULT NULL,
`Phone` varchar(100) DEFAULT NULL,
`State` varchar(100) DEFAULT NULL,
`Country` varchar(100) DEFAULT NULL,
`TechArea` varchar(100) DEFAULT NULL,
`FirmType` varchar(100) DEFAULT NULL,
`FirmSize` varchar(100) DEFAULT NULL,
`LastContactDate` date DEFAULT NULL,
`NextContactDate` date DEFAULT NULL,
`LeadDescription` varchar(200) DEFAULT NULL,
`OwnerNotes` varchar(200) DEFAULT NULL,
`SetReminder` date DEFAULT NULL,
`AdminNotes` varchar(200) DEFAULT NULL,
`LeadStatus` varchar(100) DEFAULT NULL,
`LeadAddedBy` int(100) NOT NULL,
`LeadAddedOn` datetime DEFAULT NULL,
PRIMARY KEY (`Lead_Id`),
UNIQUE KEY `FirstName` (`FirstName`,`LastName`,`Company`,`Website`,`Designation`,`Linkedin`,`Email`,`Phone`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tbl_main_lead_info`
--
INSERT INTO `tbl_main_lead_info` (`Lead_Id`, `FirstName`, `LastName`, `Company`, `Website`, `Designation`, `Linkedin`, `Email`, `Phone`, `State`, `Country`, `TechArea`, `FirmType`, `FirmSize`, `LastContactDate`, `NextContactDate`, `LeadDescription`, `OwnerNotes`, `SetReminder`, `AdminNotes`, `LeadStatus`, `LeadAddedBy`, `LeadAddedOn`) VALUES
(15, 'John', 'Doe', 'test', 'test', 'test', 'test', 'test', 'test', 'Texas', 'USA', 'test', 'Corporate', '11-50', '2020-01-09', '2020-01-10', 'Testing Description of Lead information', NULL, '2020-01-11', 'This need to be confidential by admin', 'Active', 18, '2020-01-09 16:07:17');
--
-- Dumping data for table `tbl_campaign_info`
--
INSERT INTO `tbl_campaign_info` (`Campaign_Id`, `CampaignName`, `CampaignStatus`, `CampaignDescription`, `CampaignOwnerNotes`, `CampaignAdminNotes`, `CampaignStartDate`, `CampaignEndDate`, `CampaignLead_Id`, `CampaignAddedBy`, `CampaignAddedOn`) VALUES
(16, 'Test', 'Active', 'Test', NULL, 'This is admin notes and need to be kept confidential', '2020-01-09', '2020-01-10', 15, 18, '2020-01-09'),
(17, 'Test', 'Active', 'Test ', NULL, 'NA', '2020-01-10', '2020-01-10', 15, 18, '2020-01-09');
--
-- Table structure for table `tbl_user_signup_info`
--
DROP TABLE IF EXISTS `tbl_user_signup_info`;
CREATE TABLE IF NOT EXISTS `tbl_user_signup_info` (
`User_Id` int(50) NOT NULL AUTO_INCREMENT,
`UserEmail` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`UserName` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`UserPassword` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`Admin` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`User_Id`) USING BTREE,
UNIQUE KEY `Email` (`UserEmail`),
UNIQUE KEY `UserName` (`UserName`),
UNIQUE KEY `UserEmail` (`UserEmail`,`UserName`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Dumping data for table `tbl_user_signup_info`
--
INSERT INTO `tbl_user_signup_info` (`User_Id`, `UserEmail`, `UserName`, `UserPassword`, `Admin`) VALUES
(18, 'test#gmail.com', 'test', 'test', 1),
(22, 'test1#gmail.com', 'test1', 'test1', 0),
(23, 'test2#gmail.com', 'test2', 'test2', 0);
COMMIT;
and My SQl Query is as below:
SELECT User_Id, Campaign_Id, Lead_Id
FROM tbl_campaign_info AS C,
tbl_main_lead_info AS M,
tbl_user_signup_info AS U
WHERE C.CampaignAddedBy IN ( SELECT User_Id
FROM tbl_user_signup_info AS U,
tbl_campaign_info AS C
WHERE U.User_Id = C.CampaignAddedBy)
AND M.LeadAddedBy IN (SELECT User_Id
FROM tbl_user_signup_info AS U,
tbl_main_lead_info AS M
WHERE U.User_Id = M.LeadAddedBy )
If I understand correctly, you want users campaign counts and lead counts per user. So, select campaign counts, select lead counts, join them.
You could use a full outer join of the two results for this (thus getting only those users that have at least one campain or lead), but MySQL doesn't support it. I take it that tbl_user_signup_info contains one row per user, though, so you can use this as a base table. The following query gives you a result for every user in that table.
select
u.user_id,
coalesce(c.total, 0) as campaign_count,
coalesce(l.total, 0) as lead_count
from tbl_user_signup_info u
left join
(
select campaignaddedby, count(*) as total
from tbl_campaign_info
group by campaignaddedby
) c on c.campaignaddedby = u.user_id
left join
(
select leadaddedby, count(*) as total
from tbl_main_lead_info
group by leadaddedby
) l on l.leadaddedby = u.user_id
order by u.user_id;
It seems like you can only use JOIN.
SELECT U.User_Id, Campaign_Id, Lead_Id
FROM tbl_user_signup_info AS U
JOIN tbl_campaign_info AS C ON U.User_Id = C.CampaignAddedBy
JOIN tbl_main_lead_info AS M ON U.User_Id = M.LeadAddedBy
Actually i want Total number of leads added by particular user with
there Lead Id and Total number of campaign added by that user with
there Campaign_Id using those 3 tables.
Try this:
SELECT
X.USER_ID,
X.LeadCount,
IF(Y.LEAD_ID IS NULL, 0, Y.LEAD_ID) LEAD_ID,
IF(Y.CampaignCount IS NULL, 0, Y.CampaignCount) CampaignCount
FROM (
SELECT
U.User_ID, COUNT(DISTINCT LEAD_ID) LeadCount
FROM
tbl_user_signup_info U
LEFT JOIN
tbl_main_lead_info M
ON (U.User_Id = M.LeadAddedBy)
GROUP BY
U.USER_ID
) X
LEFT JOIN
(
SELECT
M.LeadAddedBy, M.LEAD_ID, COUNT(DISTINCT Campaign_Id) CampaignCount
FROM
tbl_campaign_info C
LEFT JOIN
tbl_main_lead_info M
ON (C.CampaignAddedBy = M.LeadAddedBy)
GROUP BY
M.LeadAddedBy, M.LEAD_ID
) Y
ON (
X.USER_ID = Y.LeadAddedBy
)
SQL Fiddle link: http://sqlfiddle.com/#!9/4e138/40

How to SELECT with the given tables?

CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(16) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `permissions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`description` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `user_permissions` (
`user_id` int(11) DEFAULT NULL,
`permission_id` int(11) DEFAULT NULL,
KEY `user_id_fk` (`user_id`),
KEY `permission_id_fk` (`permission_id`),
CONSTRAINT `permission_id_fk` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`),
CONSTRAINT `user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
);
Given the above tables, how can I perform a SELECT that will return <users.name> | <true / false> if a specific user.id has permissions.id 1, 2, 3 and 4 based on the user_permissions n:m table?
Something like:
SELECT users.name, (..something..) as valid FROM user_permissions WHERE users.id 1 has 1,2,3 and 4 permissions
You need a left join of users to user_permissions, group by user and conditional aggregation:
select
u.id, u.name,
case count(up.permission_id)
when 4 then 'true'
else 'false'
end result
from users u left join user_permissions up
on up.user_id = u.id and up.permission_id in (1, 2, 3, 4)
group by u.id, u.name

how to count same rating from field in sql

I have a problem counting ratings in SQL. This is what my data looks like:
data
CREATE TABLE `restaurant` (
`id_restaurant` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id_restaurant`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
insert into `restaurant`(`id_restaurant`,`name`) values (1,'Mc Donald');
insert into `restaurant`(`id_restaurant`,`name`) values (2,'KFC');
CREATE TABLE `user` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id_user`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
insert into `user`(`id_user`,`userName`) values (1,'Audey');
CREATE TABLE `factors` (
`factor_id` int(11) NOT NULL AUTO_INCREMENT,
`factor_clean` int(11) NOT NULL DEFAULT '0',
`factor_delicious` int(11) NOT NULL DEFAULT '0',
`id_restaurant` int(11) DEFAULT NULL,
`id_user` int(11) DEFAULT NULL,
PRIMARY KEY (`factor_id`),
KEY `id_restaurant` (`id_restaurant`),
KEY `id_user` (`id_user`),
CONSTRAINT `factors_ibfk_1` FOREIGN KEY (`id_restaurant`) REFERENCES `restaurant` (`id_restaurant`),
CONSTRAINT `factors_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `user` (`id_user`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (1,1,5,1,1);
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (2,0,5,1,1);
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (3,1,5,1,1);
insert into `factors`(`factor_id`,`factor_clean`,`factor_delicious`,`id_restaurant`,`id_user`) values (4,3,3,1,1);
And the result should be like this, Show all ratings (1,2,3,4,5) and their count from the fields rating_clean, rating_delicious, and rating_clean
Thanks for your help.
but the result i get
SELECT COUNT(`factor_clean`+`factor_delicious`),'1' AS rating_1 FROM `factors` WHERE 1 GROUP BY `id_restaurant`
result not should like this
the result should not like that,
my question is, how to select just factor_clean and factor_delicious where factor_clean =1 and factor_delicious = 1
Use union all to unpivot the data and then aggregate:
select id_restaurant, rating, count(*)
from ((select r.id_restaurant, r.rating_clean as rating, r.date
from ratings r
) union all
(select r.id_restaurant, r.rating_delicious, r.date
from ratings r
) union all
(select r.id_restaurant, r.rating_clean2, r.date
from ratings r
)
) r
group by id_restaurant, rating
order by id_restaurant, rating;
For example this is solution for table with colums rating_delicious and rating_clean (only one!):
First of all you should create additional table, I called it factors:
CREATE TABLE `factors` (
`factor_id` int(11) NOT NULL AUTO_INCREMENT,
`factor_clean` int(11) NOT NULL DEFAULT '0',
`factor_delicious` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`factor_id`)
)
Next add two records:
INSERT INTO `factors` (`factor_id`, `factor_clean`, `factor_delicious`) VALUES (NULL, '1', '0'), (NULL, '0', '1');
Now you can join this tables and get results:
SELECT x.id_restaurant
, (x.rating_clean * f.factor_clean) + (x.rating_delicious * f.factor_delicious) AS rating
, count(*)
FROM your_table x
JOIN factors f
WHERE 1
GROUP
BY x.id_restaurant
, rating
In order to use next colum (rating_third), you should and column factor_third to factors, insert new row with 1 in this column and finally add something like your_table.rating_third*factors.factor_third to sum in SELECT

Generate Prefix Number ID with Condition By Group ID

First this is my table
CREATE TABLE IF NOT EXISTS `group` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `member` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`group_id` int(11) unsigned NOT NULL,
`code` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `group_id` (`group_id`),
FOREIGN KEY (`group_id`) REFERENCES `group` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I need member.id look like 001, 002, 003. . i following lpad(id, 3, 0) and case close
But how to reset 001, 002, 003 back to 001 again where have different group_id
This query have result 01-001, 01-002, 02-003, 03-004 (group.id - member.id)
SELECT CONCAT(lpad(g.id, 2, 0),'-',lpad(m.id, 3, 0)) AS id, m.name
FROM member m
LEFT JOIN group g ON g.id = m.group_id
ORDER BY m.id ASC
Please help, i need 01-001, 01-002, 02-001, 03-001 (group.id - member.id)
Or can i use trigger to make my member.code auto generate when insert to 01-001, 01-002, 02-003, 03-004 base on group.id - member.id

why category name is null when select join with enry table

i have two tables ...first is entry
CREATE TABLE IF NOT EXISTS `entry` (
`entry_id` int(11) NOT NULL AUTO_INCREMENT,
`entry_cat_id` int(11) NOT NULL,
`entry_name` varchar(255) NOT NULL,
`entry_body` text NOT NULL,
`img_url` varchar(255) NOT NULL,
`image_link` varchar(255) NOT NULL,
`entry_state` tinyint(1) DEFAULT '0',
`comment_count` int(11) NOT NULL DEFAULT '0',
`entry_count` int(11) NOT NULL DEFAULT '0',
`entry_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`entry_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=41 ;
--
-- Dumping data for table `entry`
--
INSERT INTO `entry` (`entry_id`, `entry_cat_id`, `entry_name`, `entry_body`, `img_url`, `image_link`, `entry_state`, `comment_count`, `entry_count`, `entry_created`) VALUES
(27, 15, 'title fot entry', 'content', '', '', 0, 0, 0, '2013-04-14 14:47:56');
and the escond is entry_category
CREATE TABLE IF NOT EXISTS `entry_category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(150) NOT NULL,
`slug` varchar(150) NOT NULL,
`cat_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
--
-- Dumping data for table `entry_category`
--
INSERT INTO `entry_category` (`category_id`, `category_name`, `slug`, `cat_created`) VALUES
(10, 'rrrrrrrr', 'rrrrrrr', '0000-00-00 00:00:00'),
(15, 'gggggg', 'ttttttttt', '2012-12-10 13:47:28');
when select with inner join to get the category and entry name i see category_name is null
SELECT entry.entry_id, entry_category.category_name, entry.entry_name
FROM entry
INNER JOIN entry_category
ON entry.entry_id=entry_category.category_id;
why and what i shall do to see the the category name
join is having wrong clause
you need to join on category_id but in entry table category_id is in entry_cat_id field so you can join like this
SELECT entry.entry_id, entry_category.category_name, entry.entry_name
FROM entry
INNER JOIN entry_category
ON entry.entry_cat_id=entry_category.category_id;
I think you are joining on the wrong field. You are joining on the entry.entry_id field when I think you want to be joining on the entry.entry_cat_id field.
Try changing your query to:
SELECT entry.entry_id, entry_category.category_name, entry.entry_name
FROM entry
INNER JOIN entry_category
ON entry.entry_cat_id=entry_category.category_id;
BTW -- your existing query shouldn't return any results from your sample data. To see NULL records from your entry_category table, you'd need to use an OUTER JOIN.