I have 3 tables tags,tag_type,user_alert
table structure is as follows:
CREATE TABLE `tags` (
`tag_id` int(11) NOT NULL,
`tag_name` varchar(255) CHARACTER SET utf8 NOT NULL,
`tagtype_id` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`is_deleted` tinyint(1) NOT NULL,
`created_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
CREATE TABLE IF NOT EXISTS `tag_types` (
`tagtype_id` int(11) NOT NULL,
`tagtype_name_en` varchar(100) CHARACTER SET utf8 NOT NULL,
`tagtype_name_fr` varchar(100) NOT NULL,
`display_order` int(11) NOT NULL,
`tag_color` varchar(50) CHARACTER SET utf8 NOT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
CREATE TABLE IF NOT EXISTS `user_alert` (
`user_alert_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`is_removed` enum('yes','no') DEFAULT 'no',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
I have created query, but it is not working for me.
select t.tag_name,t.tag_id,ty.tag_color,ty.tagtype_name_en as tagtype_name from tags as t INNER JOIN tag_types as ty ON t.tagtype_id=ty.tagtype_id INNER JOIN user_alert as ua ON ua.tag_id != t.tag_id where t.tagtype_id='5' and ua.user_id != '14'
I want to make query such that
I am passing tagtype_id and user_id
when I pass tagtype_id I can get all tag which belongs to that tagtyped_id and don not want to select tags in which user has subscribed.
but I am not able to do it.I know the answer will be simple but don't know how to do it.can anybody please help me?
In JOIN, you must use "=" and dont use "!=".
your condition must apply in where statement NOT in join.
Maybe this is what you are looking for (untested as i don't have this DB)
select t.tag_name,
t.tag_id,
ty.tag_color,
ty.tagtype_name_en as tagtype_name
from tags as t
INNER JOIN user_alert as ua
ON ua.tag_id <> t.tag_id
INNER JOIN tag_types as ty
ON ua.tag_id = ty.tagtype_id
where t.tagtype_id = '5'
and ua.user_id <> '14
Assuming the Table definition you supplied here is accurate; the problem is that you don't have a tagtype_name_en Field in your "tag_types" table and you are trying to access it while it doesn't exist.
// YOUR TABLE DEFINITION/COLUMNS:::
TABLE 1 - tags:
tag_id
tag_name
tagtype_id
TABLE 2 - tag_types
tagtype_id
tag_color
tagtype_name_en
TABLE 3 - user_alert
tag_id
user_id
UPDATE: ADDED GROUP BY CLAUSE
$sql = "SELECT
t.tag_name, t.tag_id,
ty.tag_color,ty.tagtype_name_en AS tagtype_name
FROM tags AS t
LEFT JOIN tag_types AS ty ON t.tagtype_id=ty.tagtype_id
LEFT JOIN user_alert AS ua ON ua.tag_id!=t.tag_id
GROUP BY t.tag_id
WHERE t.tagtype_id='5' AND ua.user_id != '14'";
Why dont you try with this <> not equal to operator instead of !=
SELECT * FROM `tags` AS `t` INNER JOIN `tag_types` AS `ty` ON t.tagtype_id = ty.tagtype_id INNER JOIN `user_alert` AS `ua` ON ua.tag_id <> t.tag_id WHERE t.tagtype_id = '5' AND ua.user_id <> '14'
UPDATED
Try this "LEFT JOIN"
SELECT * FROM `tags` AS `t` INNER JOIN `tag_types` AS `ty` ON t.tagtype_id = ty.tagtype_id LEFT JOIN `user_alert` AS `ua` ON ua.tag_id = t.tag_id WHERE t.tagtype_id = '5' AND ua.user_id <> '14' AND ua.tag_id IS NULL
Related
I have two mySql queries to get result from databases. I am trying to join them together.
Query 1:
SELECT userEwallets.id as ewalletId, users.id as userId , money_repositories.money as money, a.nestedUserId
FROM userEwallets
JOIN users ON users.id = userEwallets.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR937303656'
Query 2:
SELECT nested.id as nestedUserId
FROM userEwallets as nested
JOIN users ON users.id = nested.userId
JOIN money_repositories ON nested.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR9122331743'
and then my combination command:
SELECT userEwallets.id as ewalletId, users.id as userId , money_repositories.money as money, a.nestedUserId
FROM (
SELECT nested.id as nestedUserId
FROM userEwallets as nested
JOIN users ON users.id = nested.userId
JOIN money_repositories ON nested.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR912233'
) as a
JOIN users ON users.id = userEwallets.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE ewalletNumber = 'SHIRR93730'
I get this error:
#1054 - Unknown column 'userEwallets.id' in 'field list'
Both of commands are same but they have simple difference as ewalletNumber in where clause
UPDATE WITH DATABASE STRUCTURE
money_repositories table:
CREATE TABLE IF NOT EXISTS `money_repositories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) NOT NULL,
`ewalletId` int(11) NOT NULL,
`money` int(11) NOT NULL,
`createdAt` int(11) NOT NULL,
`updatedAt` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=4 ;
userEwallets table:
CREATE TABLE IF NOT EXISTS `userEwallets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) NOT NULL,
`ewalletNumber` varchar(15) COLLATE utf8_persian_ci NOT NULL,
`currencySymbol` varchar(5) COLLATE utf8_persian_ci NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updatedAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=7 ;
users table:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`password` varchar(65) COLLATE utf8_persian_ci NOT NULL,
`name` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`family` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`birthDay` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
`email` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`mobileNumber` varchar(15) COLLATE utf8_persian_ci NOT NULL,
`verifyCode` varchar(5) COLLATE utf8_persian_ci NOT NULL,
`photoUri` varchar(50) COLLATE utf8_persian_ci NOT NULL,
`ebanNumber` varchar(20) COLLATE utf8_persian_ci NOT NULL,
`status` tinyint(1) NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updatedAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=35 ;
userEwallets.id should be a.id in Select of your combination command. Because you are getting subquery as a.
Modified combined query;
SELECT a.id AS ewalletId,
users.id AS userId,
money_repositories.money AS money,
a.nesteduserid
FROM (SELECT nested.id AS nestedUserId,
nested.id,
nested.ewalletnumber
FROM userewallets AS nested
JOIN users
ON users.id = nested.userid
JOIN money_repositories
ON nested.id = money_repositories.ewalletid
WHERE nested.ewalletnumber = 'SHIRR912233') AS a
JOIN users
ON users.id = userewallets.userid
JOIN money_repositories
ON userewallets.id = money_repositories.ewalletid
WHERE a.ewalletnumber = 'SHIRR93730'
camleCase query fixed:
SELECT a.id AS ewalletId,
users.id AS userId,
money_repositories.money AS money,
a.nestedUserId
FROM (SELECT nested.id AS nestedUserId,
nested.id,
nested.ewalletNumber
FROM userEwallets AS nested
JOIN users ON users.id = nested.userId
JOIN money_repositories ON nested.id = money_repositories.ewalletId
WHERE nested.ewalletNumber = 'SHIRR912233') AS a
JOIN users ON users.id = nested.userId
JOIN money_repositories ON userEwallets.id = money_repositories.ewalletId
WHERE a.ewalletNumber = 'SHIRR937303'
Because you are not including table userEwallets in the query except in the sub query which won't be recognized outside the subquery. Try
UPDATE
Select
users.id as userId, userEwallets.id As ewalletId,money_repositories.money
From
users
Inner Join
userEwallets ON userEwallets.userId= users.id
Inner Join
money_repositories ON money_repositories.userId=users.id
And
money_repositories.ewalletId = userEwallets.id
Where
userEwallets.ewalletNumber = 'SHIRR93730'
So the idea is to get information from 3 tables:
R_Regle: rule table, basic info of the rule
C_Commentaire: the number of comments this rule has
I_Interet: The SUM of all upvotes and downvotes from one rule.
The structure of my I_Interet:
I_ID / I_Idregle / I_Idpseudo / I_Upvote / I_Downvote
Each row can only contain 1 upvote or 1 downvote.
so with this request:
SELECT R_Id, R_Date, R_Titre, R_Contenu, R_Etape, COUNT(C_Commentaire.C_IdRegle) AS CountComment, SUM(I_Interet.I_Up) AS Up ,SUM(I_Interet.I_Down) AS Down
FROM R_Regle
LEFT JOIN C_Commentaire ON C_Commentaire.C_IdRegle = R_Regle.R_Id
LEFT JOIN I_Interet ON I_Interet.I_IdRegle = R_Regle.R_Id
GROUP BY R_ID
I try to obtain all those information.
Problem is:
COUNT(C_Commentaire.C_IdRegle) AS CountComment
Gives me the count of all COMMENTS and UPVOTES/DOWNVOTES.
So if there are 4 comments and 3 downvotes, it will count it like that
$row['CountComment'] = 7;
But obviously, I don't want that. I want
$row['CountComment'] = 4;
Any ideas?
EDIT 1:
Here are the tables:
R_REGLE
CREATE TABLE `R_Regle` (
`R_Id` int(11) NOT NULL AUTO_INCREMENT,
`R_Auteur` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`R_Date` date NOT NULL,
`R_Titre` varchar(150) CHARACTER SET latin1 NOT NULL,
`R_Contenu` text CHARACTER SET latin1 NOT NULL,
`R_Etape` int(11) NOT NULL DEFAULT '1' COMMENT '1 = discussion 2= VOTE',
PRIMARY KEY (`R_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
C_COMMENTAIRE
CREATE TABLE `C_Commentaire` (
`C_Id` int(11) NOT NULL AUTO_INCREMENT,
`C_Date` datetime NOT NULL,
`C_Pseudo` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`C_Contenu` text COLLATE utf8_unicode_ci NOT NULL,
`C_IdRegle` int(11) NOT NULL,
PRIMARY KEY (`C_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I_INTERET
CREATE TABLE `I_Interet` (
`I_Id` int(11) NOT NULL AUTO_INCREMENT,
`I_IdRegle` int(11) NOT NULL,
`I_Pseudo` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`I_Up` tinyint(1) NOT NULL DEFAULT '0',
`I_Down` tinyint(1) DEFAULT '0',
PRIMARY KEY (`I_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I can suggest using correlated query.. not sure if thats what you want since you didn't post any table structures its hard to know :
SELECT R_Id, R_Date, R_Titre, R_Contenu, R_Etape,
(select COUNT(*) from C_Commentaire where C_Commentaire.C_IdRegle = R_Regle.R_Id) AS CountComment,
SUM(I_Interet.I_Up) AS Up ,
SUM(I_Interet.I_Down) AS Down
FROM R_Regle
LEFT JOIN I_Interet ON I_Interet.I_IdRegle = R_Regle.R_Id
GROUP BY R_ID
Write your query as below:
SELECT R_Regle.R_Id, R_Regle.R_Date, R_Regle.R_Titre, R_Regle.R_Contenu, R_Regle.R_Etape,
COUNT(C_Commentaire.C_IdRegle) AS CommentCounts,
SUM(I_Interet.I_Up) AS Up,
SUM(I_Interet.I_Down) AS Down
FROM R_Regle
LEFT JOIN C_Commentaire ON R_Regle.R_Id = C_Commentaire.C_IdRegle
LEFT JOIN I_Interet ON R_Regle.R_Id = I_Interet.I_IdRegle
GROUP BY R_Regle.R_ID
In your query there are duplicate records arisen when you make a left join among the three tables.
The following query is one of few ways to achieve your desired result.
SELECT
t.*,
SUM(I_Interet.I_Up) AS Up,
SUM(I_Interet.I_Down) AS Down
FROM
(
SELECT
R_Id,
R_Date,
R_Titre,
R_Contenu,
R_Etape,
COUNT(c_commentaire.C_IdRegle) countComment
FROM
R_Regle LEFT JOIN C_Commentaire ON C_Commentaire.C_IdRegle = R_Regle.R_Id
GROUP BY R_Regle.R_Id ) t
LEFT JOIN I_Interet ON I_Interet.I_IdRegle = t.R_Id
GROUP BY t.R_Id
I have a notifcation system like in Facebook using the following MySQL statement:
SELECT
n.`id`,n.`content_id`,n.`site_id`,n.`creator_uid`,n.`type`,
nu.`id` AS nuid, nu.`uid` AS nu_uid, nu.`date`,
nr.`id` AS nrid, nr.`uid` AS nr_uid, nr.`is_read`,
u.`gender`
FROM `notification` AS n
LEFT JOIN `notification_user` AS nu ON nu.`nid` = n.`id`
LEFT JOIN `notification_read` AS nr ON nr.`nid` = n.`id`
LEFT JOIN `users` AS u ON u.`id` = nu.`uid`
WHERE
nu.`uid` != '".$_SESSION['uid']."' AND nr.`uid` = '".$_SESSION['uid']."'
OR
(
nu.`uid` = '".$_SESSION['uid']."' AND n.`type` = 'credits'
)
ORDER BY date DESC, nu.`id` DESC
It should only display the notifications for this specific user I'm logged in as. But now I've more than 22500 records on the notification table and I'm always getting an "maximum execution time exceeded" error.
Can I change this query somehow to reduce the time getting the wanted records? Maybe remove the join and execute more queries?
EDIT: Added Table Overview
CREATE TABLE IF NOT EXISTS `notification` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content_id` int(11) NOT NULL,
`site_id` int(11) NOT NULL,
`creator_uid` int(11) NOT NULL,
`type` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22759 ;
.
CREATE TABLE IF NOT EXISTS `notification_read` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nid` int(11) NOT NULL,
`uid` int(11) NOT NULL,
`is_read` tinyint(4) NOT NULL,
PRIMARY KEY (`id`),
KEY `nid` (`nid`),
KEY `nid_2` (`nid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=45342 ;
.
CREATE TABLE IF NOT EXISTS `notification_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nid` int(11) NOT NULL,
`uid` int(11) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22813 ;
Splitting the statement up in to a pair of SELECTs, and UNIONing the results together:-
(SELECT
n.`id`,n.`content_id`,n.`site_id`,n.`creator_uid`,n.`type`,
nu.`id` AS nuid, nu.`uid` AS nu_uid, nu.`date`,
nr.`id` AS nrid, nr.`uid` AS nr_uid, nr.`is_read`,
u.`gender`
FROM `notification` AS n
INNER JOIN `notification_user` AS nu ON nu.`nid` = n.`id`
LEFT JOIN `notification_read` AS nr ON nr.`nid` = n.`id`
LEFT JOIN `users` AS u ON u.`id` = nu.`uid`
WHERE nu.`uid` = '".$_SESSION['uid']."' AND n.`type` = 'credits')
UNION
(SELECT
n.`id`,n.`content_id`,n.`site_id`,n.`creator_uid`,n.`type`,
nu.`id` AS nuid, nu.`uid` AS nu_uid, nu.`date`,
nr.`id` AS nrid, nr.`uid` AS nr_uid, nr.`is_read`,
u.`gender`
FROM `notification` AS n
LEFT JOIN `notification_user` AS nu ON nu.`nid` = n.`id`
INNER JOIN `notification_read` AS nr ON nr.`nid` = n.`id`
LEFT JOIN `users` AS u ON u.`id` = nu.`uid`
WHERE nu.`uid` != '".$_SESSION['uid']."' AND nr.`uid` = '".$_SESSION['uid']."')
ORDER BY date DESC, nu.`id` DESC
This should allow MySQL to use indexes effectively on each part of the query. The first part of the query requires a notification_user record so you can use an INNER JOIN there, while the 2nd requires a notification_read record so you can use an INNER JOIN there. Both of those should cut the number of rows to process.
Add an index on the uid field on the notification_user table
Add an index on the uid field on the notification_read table
I have a photo contest app where users can vote. I would like to select all of the contests where the logged in user has not voted yet.
So I have two tables.
The "contest" table :
CREATE TABLE `contest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`desc` text NOT NULL,
`created_date` datetime NOT NULL,
`started_date` datetime NOT NULL,
`nb_user_min` int(11) NOT NULL,
`nb_photo_max` int(11) NOT NULL,
`nb_photo_per_user` int(11) NOT NULL,
`duration` int(11) DEFAULT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The "contest_vote" table :
CREATE TABLE `contest_vote` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`pic_id` int(11) DEFAULT NULL,
`contest_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`date` datetime DEFAULT NULL,
`ip` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
So to be clear, I want to get the number (or the list) of contests where the user has not voted yet. So I have tried with a LEFT JOIN but it doesn't return the good set of result. Here it the query I have until now :
SELECT DISTINCT c.id, c.title, cv.user_id
FROM contest c
LEFT JOIN contest_vote cv
ON cv.contest_id = c.id AND cv.user_id != ?
GROUP BY contest_id
("?" represents the user_id parameter).
Can you help me to solve this?
This is pretty simple do with a subquery. Just grab all contest without where user is voted like this:
SELECT DISTINCT c.id, c.title
FROM contest c
WHERE c.id NOT IN (SELECT DISTINCT cv.contest_id FROM contest_vote cv WHERE cv.user_id = ?)
Try this one :
SELECT DISTINCT c.id, c.title, cv.user_id
FROM contest c
LEFT JOIN contest_vote cv ON cv.contest_id = c.id AND cv.user_id != ? WHERE c.user_id = ?
GROUP BY contest_id
these are my tables. first one is appusers table.
CREATE TABLE IF NOT EXISTS `appusers` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(50) NOT NULL,
`is_active` tinyint(2) NOT NULL DEFAULT '0',
`zip` varchar(20) NOT NULL,
`city` text NOT NULL,
`country` text NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
second table is stickeruses table.
CREATE TABLE IF NOT EXISTS `stickeruses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`sticker_id` int(11) NOT NULL,
`count` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;
Third table is Devices
CREATE TABLE IF NOT EXISTS `devices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`regid` varchar(300) NOT NULL,
`imei` varchar(50) NOT NULL,
`device_type` tinyint(2) NOT NULL,
`notification` tinyint(2) NOT NULL DEFAULT '1',
`is_active` tinyint(2) NOT NULL DEFAULT '0',
`activationcode` int(6) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;
I Want to find the Sum(stickeruses.count) and COUNT(devices.id) for all appusers.
Here is my query.
SELECT `Appuser`.`id`, `Appuser`.`email`, `Appuser`.`country`, `Appuser`.`created`,
`Appuser`.`is_active`, SUM(`Stickeruse`.`count`) AS total, COUNT(`Device`.`id`)
AS tdevice
FROM `stickerapp`.`appusers` AS `Appuser`
LEFT JOIN `stickerapp`.`stickeruses` AS `Stickeruse`
ON (`Stickeruse`.`user_id`=`Appuser`.`id`)
INNER JOIN `stickerapp`.`devices` AS `Device`
ON (`Device`.`user_id`=`Appuser`.`id`)
WHERE `Appuser`.`is_active` = 1
GROUP BY `Appuser`.`id`
LIMIT 10
When I am applying each join separately the results are right, but I want to combine both joins. And when I am doing it then results are wrong. please help.
When mixing JOIN and LEFT JOIN it is a good idea to use parentheses to make it clear what your intent is.
I don't know what you need, but these syntaxes might give you different results:
FROM a LEFT JOIN ( b JOIN c ON b..c.. ) bc ON a..bc..
FROM ( a LEFT JOIN b ON a..b.. ) ab JOIN c ON ab..c..
Also, you can rearrange them do FROM a JOIN c LEFT JOIN b (plus parentheses) or any of several other arrangements. Granted, some pairs rearrangements are equivalent.
Also, beware; aggregates (such as SUM()) get inflated values when JOINing. Think of it this way: first the JOINs get all appropriate combinations of rows from the tables, then the SUM adds them up. With that in mind, see if this works better:
SELECT a.`id`, a.`email`, a.`country`, a.`created`, a.`is_active`,
( SELECT SUM(`count`)
FROM stickerapp.stickeruses
WHERE user_id = a.id
) AS total,
( SELECT COUNT(*)
FROM stickerapp.devices
WHERE user_id = a.id
) AS tdevice
FROM stickerapp.`appusers` AS a
WHERE a.`is_active` = 1
GROUP BY a.`id`
LIMIT 10