group by different user names other than the receiver name - mysql

I have the following "tblA" and "tblB" table:
CREATE TABLE if not exists tblA
(
id int(11) NOT NULL auto_increment,
sender varchar(255),
receiver varchar(255),
msg varchar(255),
date timestamp,
PRIMARY KEY (id)
);
CREATE TABLE if not exists tblB
(
id int(11) NOT NULL auto_increment,
sno varchar(255),
name varchar(255),
PRIMARY KEY (id)
);
INSERT INTO tblA (sender, receiver, msg,date) VALUES
('1', '2', 'buzz ...','2011-08-21 14:11:09'),
('1', '2', 'test ...','2011-08-21 14:12:19'),
('1', '2', 'check ...','2011-08-21 14:13:29'),
('1', '1', 'test2 ...','2011-08-21 14:14:09'),
('2', '1', 'check2 ...','2011-08-21 14:15:09'),
('2', '1', 'test3 ...','2011-08-21 14:16:09'),
('1', '2', 'buzz ...','2011-08-21 14:17:09'),
('1', '2', 'test ...','2011-08-21 14:18:19'),
('1', '2', 'check ...','2011-08-21 14:19:29'),
('1', '1', 'test2 ...','2011-08-21 14:10:09'),
('3', '1', 'check2 ...','2011-08-21 14:21:09'),
('3', '1', 'test3 ...','2011-08-21 14:22:09'),
('3', '2', 'buzz ...','2011-08-21 14:24:09'),
('3', '2', 'test ...','2011-08-21 14:25:19'),
('1', '3', 'check ...','2011-08-21 14:26:29'),
('1', '3', 'test2 ...','2011-08-21 14:27:09'),
('2', '3', 'check2 ...','2011-08-21 14:28:09'),
('2', '3', 'test3 ...','2011-08-21 14:29:09'),
('1', '2', 'check3 ...','2011-08-21 14:23:09'),
('1', '4', 'test2 ...','2011-08-21 14:27:09'),
('1', '5', 'test2 ...','2011-08-21 14:27:09'),
('2', '6', 'check2 ...','2011-08-21 14:28:09'),
('2', '7', 'test3 ...','2011-08-21 14:29:09'),
('8', '2', 'check3 ...','2011-08-21 14:23:09');
INSERT INTO tblB (sno, name) VALUES
('1', 'Aa'),
('2', 'Bb'),
('3', 'Cc'),
('4', 'Dd'),
('5', 'Ee'),
('6', 'Ff'),
('7', 'Gg'),
('8', 'Hh');
I want the list of people Aa is talking to ordered by Date (Last entry for each user group).
Bb
Cc
Dd
Ee
Ff
Gg
Hh
I'm getting stuck on how to Group them without including "Aa" in one query.

If I understand correctly:
select b.name
from tblA a join
tblB b
on b.id in (a.receiver, a.sender)
where b.name <> 'Aa'
order by date desc;
Here is a SQL Fiddle. Note that your above code has a few semicolons that should be commas.
EDIT:
If you want the most recent date, then use:
select b.name
from tblA a join
tblB b
on b.id in (a.receiver, a.sender)
where b.name <> 'Aa'
group by b.name
order by max(date) desc;
EDIT II:
To filter by rows that just have 'Aa':
select b.name
from tblA a join
tblB b
on b.id in (a.receiver, a.sender)
where b.name <> 'Aa' and
exists (select 1
from tblB b2
where b2.name = 'Aa' and
b2.id in (a.receiver, a.sender)
)
group by b.name
order by max(date) desc;

Related

Absenteeism query showing date as employee is absent

I'm trying to generate an absenteeism query with the date employees were absent.
Table "ci_admin":
DROP TABLE IF EXISTS `ci_admin`;
CREATE TABLE `ci_admin` (
`admin_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_role_id` int(11) NOT NULL,
`department_id` int(11) NOT NULL,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`emp_photo` varchar(255) NOT NULL,
PRIMARY KEY (`admin_id`)
);
INSERT INTO `ci_admin` VALUES ('1', '20170079320', '1', '1', 'AHMED', 'MAKOKHA', '125.jpg'), ('2', '20170079321', '1', '1', 'MARIAM', 'AHMED', '2.png'), ('3', '20170080091', '2', '3', 'SALAHUDEEN', 'AHMED', '3.png'), ('4', '20130080083', '2', '2', 'NURU', 'ABBAS', '4.png'), ('5', '20170080092', '5', '1', 'HIRBO', 'ALI', '5.png');
Table "ci_department":
DROP TABLE IF EXISTS `ci_department`;
CREATE TABLE `ci_department` (
`department_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_id` int(11) NOT NULL,
`department_name` varchar(225) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`department_id`)
);
INSERT INTO `ci_department` VALUES ('1', 'INFORMATION COMMUNICATION TECHNOLOGY'), ('2', 'HUMAN RESOURCE'), ('3', 'PROCUREMENT');
Table "ci_admnin_roles":
DROP TABLE IF EXISTS `ci_admin_roles`;
CREATE TABLE `ci_admin_roles` (
`admin_role_id` int(11) NOT NULL AUTO_INCREMENT,
`admin_role_title` varchar(30) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`admin_role_id`)
);
INSERT INTO `ci_admin_roles` VALUES ('1', 'Super Admin'), ('2', 'ADMIN'), ('3', 'ACCOUNTANT'), ('4', 'OPERATOR'), ('5', 'IT OFFICER'), ('6', 'APPLICANT');
Table "ci_attendance":
DROP TABLE IF EXISTS `ci_attendance`;
CREATE TABLE `ci_attendance` (
`attendance_id` int(11) NOT NULL AUTO_INCREMENT,
`attendance_date` date NOT NULL,
`admin_id` int(11) NOT NULL,
`check_in` time NOT NULL,
`check_out` time NOT NULL,
PRIMARY KEY (`attendance_id`)
);
INSERT INTO `ci_attendance` VALUES ('1', '2022-12-22', '1', '08:15:00', '18:30:00'), ('2', '2022-12-22', '2', '09:00:00', '18:30:00'), ('3', '2022-12-22', '3', '10:00:00', '19:30:00'), ('4', '2020-02-21', '1', '10:30:00', '20:30:00'), ('5', '2023-12-12', '2', '10:00:00', '21:30:00'), ('6', '2023-12-23', '3', '10:00:00', '22:30:00'), ('7', '2023-12-22', '3', '08:00:00', '17:30:00'), ('8', '2021-02-21', '2', '09:00:00', '18:30:00'), ('9', '2024-12-12', '3', '10:00:00', '19:30:00'), ('10', '2024-12-23', '1', '10:00:00', '20:30:00'), ('11', '2024-12-22', '2', '10:00:00', '21:30:00'), ('12', '2022-02-21', '3', '10:00:00', '22:30:00'), ('13', '2022-12-12', '1', '08:00:00', '17:30:00'), ('14', '2022-12-23', '3', '09:00:00', '18:30:00'), ('15', '2022-12-02', '2', '10:00:00', '19:30:00'), ('16', '2020-02-21', '1', '09:45:00', '22:00:00'), ('17', '2023-12-12', '2', '10:00:00', '21:30:00'), ('18', '2020-12-23', '3', '10:00:00', '22:30:00'), ('19', '2023-12-22', '1', '08:00:00', '17:30:00'), ('20', '2021-02-21', '2', '09:00:00', '18:30:00'), ('21', '2024-12-03', '2', '10:00:00', '19:30:00'), ('22', '2021-12-23', '1', '10:00:00', '20:30:00'), ('23', '2024-10-22', '3', '10:00:00', '21:30:00'), ('24', '2022-02-21', '3', '10:00:00', '22:30:00'), ('25', '2022-12-12', '1', '08:00:00', '17:30:00'), ('26', '2022-09-23', '2', '09:00:00', '18:30:00'), ('27', '2022-12-22', '4', '10:00:00', '19:30:00'), ('28', '2020-02-21', '1', '10:00:00', '20:30:00'), ('29', '2023-04-12', '5', '10:00:00', '21:30:00'), ('30', '2023-12-03', '4', '10:00:00', '22:30:00'), ('31', '2023-12-22', '1', '08:00:00', '17:30:00'), ('32', '2021-02-21', '5', '09:00:00', '18:30:00'), ('33', '2024-12-12', '4', '10:00:00', '19:30:00');
My current MySQL query which displays employees who are absent does not include the date when the employee is absent:
SELECT ci_admin.emp_no,
ci_admin.firstname,
ci_admin.lastname,
ci_admin.emp_photo,
ci_department.department_name,
ci_admin_roles.admin_role_title
FROM ci_admin
INNER JOIN ci_department ON ci_department.department_id = ci_admin.department_id
INNER JOIN ci_admin_roles on ci_admin_roles.admin_role_id=ci_admin.admin_role_id
WHERE ci_admin.is_deleted = 0
AND ci_admin.admin_id NOT IN (SELECT ci_attendance.admin_id
FROM ci_attendance
WHERE ci_attendance.attendance_date BETWEEN '2023-01-01' AND '2023-01-31')
ORDER BY ci_admin.department_id
I want the specific date that an employee is absent to be included in my query. How can I modify my query?

What should I change not to get error 1136 in MySQL?

CREATE TABLE IF NOT EXISTS `mushu`.`episode` (
`id_episode` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`number` VARCHAR(5) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`episode_length` VARCHAR(10) NOT NULL,
`location` VARCHAR(150) NOT NULL,
`season_id_season` INT UNSIGNED NOT NULL,
`season_tv_show_id_tv_show` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id_episode`, `season_id_season`, `season_tv_show_id_tv_show`),
INDEX `fk_episode_season1_idx` (`season_id_season` ASC,
`season_tv_show_id_tv_show` ASC) VISIBLE,
CONSTRAINT `fk_episode_season1`
FOREIGN KEY (`season_id_season` , `season_tv_show_id_tv_show`)
REFERENCES `mushu`.`season` (`id_season` , `tv_show_id_tv_show`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
INSERT INTO episode VALUES
(1, '1', 'Pilot', '45 min', 'E:\MuShu', '1', '1'),
(2, '2', 'Another Trip Around the Sun', '45 min', 'E:\MuShu', '1', '1'),
(3, '1', 'Pilot', '43 min', 'E:\MuShu', '1', '2'),
(4, '2', 'A Stray Howl', '43 min', 'E:\MuShu', '1', '2'),
(5, '3', 'Eight Slim Grins', '43 min', 'E:\MuShu', '1', '2'),
(6, '1', 'Burnt Food', '42 min', 'E:\MuShu', '1', '3'),
(7, '2', 'Mount Rushmore', '42 min', 'E:\MuShu', '1', '3'),
(8, '1', 'Impact', '56 min', 'E:\MuShu', '1', '4'),
(9, '1', 'Pilot', '44 min', 'E:\MuShu', '1', '5'),
(10, '2', 'The Lorelais First Day at Chilton', '44 min', 'E:\MuShu', '1', '5'),
(11, '1', 'Pilot', '42 min', 'E:\MuShu', '1', '6'),
(12, '2', 'Independence Day', '42 min', 'E:\MuShu', '1', '6'),
(13, '3', 'Comrades in Arms', '42 min', 'E:\MuShu', '1', '6'),
(14, '1', 'Pilot', '22 min', 'E:\MuShu', '1', '7'),
(15, '2', 'Rockets, Communists and the Dewey Decimal System', '22 min', 'E:\MuShu', '1', '7'),
(16, '1', 'Always and Forever', '45 min', '1', '8'),
(17, '1', 'Pilot', '43 min', '1', '9'),
(18, '2', 'Its All Her Fault', '1', '9'),
(19, '1', 'Pilot', '41 min', '1', '10'),
(20, '2', 'The Jenna Thing', '41 min', '1', '10');
Error Code: 1136. Column count doesn't match value count at row 16.
How do I solve this error?
Some values a 7 long others are 6. They all must be the same (7). If you look at row 16 like the error says you'll see.

Get the the product with highest sells in each store - keep getting wrong products but right quantities

For the following table I want to get a table that shows the most sold product in each store
Same product can be the most sold in more than 1 store.
This is the query I tried:
SELECT StoreID, ProductID, MAX(Quantity), TotalPrice FROM MyTable GROUP BY StoreID LIMIT 0, 99999;
The answer I get is:
|StoreID|ProductID|MAX(Quantity)|TotalPrice
'1', '1', '566', '288.0000'
'2', '1', '782', '648.0000'
'3', '1', '473', '172.8000'
The problem:
I've checked and the max quantities are correct
but for some reason it shows the ID '1' for every product even that it's not the most sold product in any store.
For this table (Called 'MyTable'):
|StoreID|TotalPrice|ProductID|Quantity
'1', '288.0000', '1', '167'
'1', '760.0000', '2', '417'
'1', '400.0000', '3', '70'
'1', '352.0000', '4', '218'
'1', '1105.0000', '5', '117'
'1', '600.0000', '6', '156'
'1', '300.0000', '7', '193'
'1', '960.0000', '8', '84'
'1', '1552.0000', '9', '36'
'1', '558.0000', '10', '115'
'1', '201.6000', '11', '218'
'1', '152.0000', '12', '8'
'1', '9.6000', '13', '187'
'1', '167.4000', '14', '47'
'1', '62.0000', '15', '42'
'1', '417.0000', '16', '451'
'1', '936.0000', '17', '345'
'1', '450.0000', '18', '112'
'1', '7.3000', '19', '68'
'1', '1701.0000', '20', '66'
'1', '80.0000', '21', '337'
'1', '100.8000', '22', '58'
'1', '151.2000', '23', '275'
'1', '43.2000', '24', '431'
'1', '44.8000', '25', '171'
'1', '1245.0000', '26', '297'
'1', '526.5000', '27', '185'
'1', '1528.8000', '28', '127'
'1', '1485.0000', '29', '61'
'1', '124.2000', '30', '158'
'1', '600.0000', '31', '566'
'1', '153.6000', '32', '91'
'1', '120.0000', '33', '294'
'1', '112.0000', '34', '62'
'1', '57.6000', '35', '249'
'1', '456.0000', '36', '200'
'1', '208.0000', '37', '18'
'1', '4216.0000', '38', '86'
'1', '57.6000', '39', '192'
'1', '735.0000', '40', '431'
'1', '123.2000', '41', '220'
'1', '22.4000', '42', '183'
'1', '920.0000', '43', '141'
'1', '325.5000', '44', '281'
'1', '114.0000', '45', '360'
'1', '288.0000', '46', '249'
'1', '418.0000', '47', '156'
'1', '191.2500', '48', '45'
'1', '240.0000', '49', '45'
'1', '406.2500', '50', '25'
'1', '1696.0000', '51', '271'
'1', '112.0000', '52', '146'
'1', '1048.0000', '53', '202'
'1', '59.0000', '54', '180'
'1', '384.0000', '55', '147'
'1', '2128.0000', '56', '428'
'1', '234.0000', '57', '146'
'1', '318.0000', '58', '201'
'1', '3080.0000', '59', '504'
'1', '544.0000', '60', '433'
'1', '364.8000', '61', '221'
'1', '591.0000', '62', '247'
'1', '2808.0000', '63', '187'
'1', '1330.0000', '64', '194'
'1', '336.0000', '65', '250'
'1', '816.0000', '66', '110'
'1', '56.0000', '67', '105'
'1', '30.0000', '68', '245'
'1', '432.0000', '69', '305'
'1', '252.0000', '70', '254'
'1', '344.0000', '71', '326'
'1', '556.0000', '72', '400'
'1', '240.0000', '73', '143'
'1', '400.0000', '74', '70'
'1', '186.0000', '75', '490'
'1', '216.0000', '76', '348'
'1', '104.0000', '77', '255'
'2', '648.0000', '1', '317'
'2', '608.0000', '2', '453'
'2', '160.0000', '3', '128'
'2', '422.4000', '4', '109'
'2', '85.4000', '5', '124'
'2', '150.0000', '6', '77'
'2', '240.0000', '7', '332'
'2', '2240.0000', '8', '222'
'2', '4850.0000', '9', '53'
'2', '396.8000', '10', '435'
'2', '403.2000', '11', '293'
'2', '570.0000', '12', '107'
'2', '96.0000', '13', '477'
'2', '279.0000', '14', '224'
'2', '155.0000', '15', '60'
'2', '139.0000', '16', '294'
'2', '468.0000', '17', '416'
'2', '2000.0000', '18', '218'
'2', '109.5000', '19', '422'
'2', '2592.0000', '20', '219'
'2', '160.0000', '21', '286'
'2', '201.6000', '22', '204'
'2', '180.0000', '23', '187'
'2', '54.0000', '24', '363'
'2', '134.4000', '25', '120'
'2', '249.0000', '26', '164'
'2', '1755.0000', '27', '80'
'2', '1019.2000', '28', '281'
'2', '1980.0000', '29', '458'
'2', '165.6000', '30', '175'
'2', '200.0000', '31', '533'
'2', '1024.0000', '32', '176'
'2', '50.0000', '33', '233'
'2', '156.8000', '34', '158'
'2', '288.0000', '35', '348'
'2', '608.0000', '36', '359'
'2', '468.0000', '37', '18'
'2', '4216.0000', '38', '292'
'2', '604.8000', '39', '317'
'2', '588.0000', '40', '518'
'2', '77.0000', '41', '301'
'2', '112.0000', '42', '223'
'2', '552.0000', '43', '298'
'2', '248.0000', '44', '136'
'2', '380.0000', '45', '40'
'2', '48.0000', '46', '230'
'2', '121.6000', '47', '197'
'2', '153.0000', '48', '15'
'2', '640.0000', '49', '344'
'2', '195.0000', '50', '101'
'2', '1484.0000', '51', '328'
'2', '44.8000', '52', '198'
'2', '393.0000', '53', '320'
'2', '59.0000', '54', '295'
'2', '403.2000', '55', '283'
'2', '121.6000', '56', '486'
'2', '249.6000', '57', '202'
'2', '318.0000', '58', '102'
'2', '660.0000', '59', '630'
'2', '1088.0000', '60', '782'
'2', '2052.0000', '61', '177'
'2', '591.0000', '62', '399'
'2', '280.8000', '63', '223'
'2', '931.0000', '64', '397'
'2', '252.0000', '65', '229'
'2', '408.0000', '66', '95'
'2', '210.0000', '67', '55'
'2', '200.0000', '68', '298'
'2', '518.4000', '69', '232'
'2', '240.0000', '70', '299'
'2', '34.4000', '71', '258'
'2', '667.2000', '72', '200'
'2', '300.0000', '73', '85'
'2', '168.0000', '74', '92'
'2', '37.2000', '75', '542'
'2', '86.4000', '76', '367'
'2', '124.8000', '77', '236'
'3', '172.8000', '1', '344'
'3', '304.0000', '2', '187'
'3', '240.0000', '3', '130'
'3', '1100.0000', '4', '126'
'3', '204.0000', '5', '57'
'3', '120.0000', '6', '68'
'3', '360.0000', '7', '238'
'3', '400.0000', '8', '66'
'3', '582.0000', '9', '6'
'3', '595.2000', '10', '192'
'3', '168.0000', '11', '195'
'3', '364.8000', '12', '229'
'3', '48.0000', '13', '227'
'3', '167.4000', '14', '133'
'3', '248.0000', '15', '20'
'3', '486.5000', '16', '413'
'3', '249.6000', '17', '217'
'3', '600.0000', '18', '209'
'3', '131.4000', '19', '233'
'3', '1814.4000', '20', '28'
'3', '80.0000', '21', '393'
'3', '840.0000', '22', '86'
'3', '288.0000', '23', '118'
'3', '100.8000', '24', '331'
'3', '112.0000', '25', '27'
'3', '597.6000', '26', '292'
'3', '877.5000', '27', '100'
'3', '728.0000', '28', '232'
'3', '990.0000', '29', '227'
'3', '1242.0000', '30', '279'
'3', '150.0000', '31', '298'
'3', '320.0000', '32', '30'
'3', '40.0000', '33', '228'
'3', '224.0000', '34', '286'
'3', '1440.0000', '35', '286'
'3', '380.0000', '36', '246'
'3', '20.8000', '37', '89'
'3', '10540.0000', '38', '245'
'3', '86.4000', '39', '284'
'3', '882.0000', '40', '154'
'3', '192.5000', '41', '460'
'3', '98.0000', '42', '291'
'3', '1472.0000', '43', '141'
'3', '620.0000', '44', '184'
'3', '228.0000', '45', '108'
'3', '144.0000', '46', '69'
'3', '114.0000', '47', '132'
'3', '714.0000', '48', '78'
'3', '560.0000', '49', '131'
'3', '195.0000', '50', '109'
'3', '127.2000', '51', '287'
'3', '112.0000', '52', '156'
'3', '262.0000', '53', '200'
'3', '29.5000', '54', '280'
'3', '2304.0000', '55', '473'
'3', '60.8000', '56', '349'
'3', '312.0000', '57', '86'
'3', '848.0000', '58', '231'
'3', '1320.0000', '59', '362'
'3', '952.0000', '60', '362'
'3', '712.5000', '61', '205'
'3', '472.8000', '62', '437'
'3', '175.5000', '63', '35'
'3', '239.4000', '64', '149'
'3', '672.0000', '65', '266'
'3', '136.0000', '66', '34'
'3', '336.0000', '67', '24'
'3', '100.0000', '68', '256'
'3', '28.8000', '69', '177'
'3', '60.0000', '70', '264'
'3', '516.0000', '71', '473'
'3', '174.0000', '72', '206'
'3', '525.0000', '73', '65'
'3', '288.0000', '74', '135'
'3', '37.2000', '75', '123'
'3', '475.2000', '76', '266'
'3', '156.0000', '77', '300'
Try something like below:
SELECT *
FROM MyTable WHERE (storeID,Quantity) IN
( SELECT storeID, MAX(Quantity)
FROM MyTable
GROUP BY storeID
)
The query you wrote is ambiguous. ProductID is not the product that had the max quantity. Think about this query:
SELECT StoreID, ProductID, MAX(Quantity), MIN(Quantity), TotalPrice
FROM MyTable
GROUP BY StoreID LIMIT 0, 99999;
Which ProductID do you expect this query to return? The product that has the max quantity or the product that has the min quantity?
Or this:
SELECT StoreID, ProductID, AVG(Quantity), TotalPrice
FROM MyTable
GROUP BY StoreID LIMIT 0, 99999;
Perhaps no product has a quantity that is exactly the average quantity.
This type of SQL query is technically illegal and it's an error in most databases other than MySQL. MySQL allows it, and trusts you to know when you're writing such an ambiguous query.
Tip: Every column of your select-list must be either:
Inside an aggregate function
Named in the GROUP BY clause.
Any other column will not return a reliable value.
You're asking for a query that has been dubbed the greatest-n-per-group problem. This question comes up frequently on Stack Overflow, and it has been answered many times. Here's a couple of my past answers that got a lot of upvotes:
Retrieving the last record in each group
Fetch the row which has the Max value for a column
It matches because you're grouping by StoreID. MAX is a GROUP BY function, so it returns the MAX(Quantity) for each StoreID.
For ProductID and TotalPrice, the query is just returning the values from the first row for each store, because there's nothing telling it to read the values from the row where Quantity=MAX(Quantity)
TheUknown's query, on the other hand, first finds the MAX(Quantity) for each store, then does a second query to return all the fields from the row where Quantity=MAX(Quantity)

the group by in mysql5.7

I find the same sql run in mysql5.7 and mysql5.5 but the result is different.
the sql like this:
SELECT t2.* FROM (SELECT t1.* FROM t_user t1 ORDER BY t1.id desc) AS t2 GROUP BY t2.type;
The result in mysql5.7:
The result in mysql5.5:
the sql script like this:
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`gender` varchar(255) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`birth` datetime DEFAULT NULL,
`is_delete` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;
INSERT INTO `t_user` VALUES ('1', 'James', '0594-5397864', '0', '3', '2016-01-30 19:01:09', '1');
INSERT INTO `t_user` VALUES ('2', 'Hayes', '0594-5392419', '1', '4', '2015-12-24 11:12:27', '1');
INSERT INTO `t_user` VALUES ('3', 'Diana', '0594-5393520', '1', '5', '2016-03-21 13:03:50', '0');
INSERT INTO `t_user` VALUES ('4', 'Rajah', '0594-5399812', '1', '4', '2015-11-26 02:11:35', '0');
INSERT INTO `t_user` VALUES ('5', 'Daria', '0594-5397571', '0', '4', '2016-01-18 11:01:11', '1');
INSERT INTO `t_user` VALUES ('6', 'Lee', '0594-5394539', '1', '1', '2015-10-23 08:10:23', '1');
INSERT INTO `t_user` VALUES ('7', 'Cameran', '0594-5392867', '0', '4', '2016-11-16 12:11:08', '0');
INSERT INTO `t_user` VALUES ('8', 'Wylie', '0594-5395349', '0', '5', '2017-07-06 04:07:27', '0');
INSERT INTO `t_user` VALUES ('9', 'Bertha', '0594-5395287', '1', '1', '2017-02-08 12:02:45', '1');
INSERT INTO `t_user` VALUES ('10', 'Fletcher', '0594-5399246', '0', '4', '2015-09-03 20:09:33', '0');
INSERT INTO `t_user` VALUES ('11', 'Conan', '0594-5391546', '1', '5', '2017-05-15 09:05:23', '0');
INSERT INTO `t_user` VALUES ('12', 'Raymond', '0594-5399666', '0', '3', '2015-10-20 05:10:05', '1');
INSERT INTO `t_user` VALUES ('13', 'Noel', '0594-5397392', '1', '4', '2017-05-26 03:05:56', '0');
INSERT INTO `t_user` VALUES ('14', 'Miriam', '0594-5399081', '0', '2', '2016-05-21 02:05:09', '0');
INSERT INTO `t_user` VALUES ('15', 'Maya', '0594-5397242', '0', '3', '2016-10-24 02:10:50', '1');
INSERT INTO `t_user` VALUES ('16', 'Winifred', '0594-5395142', '1', '1', '2017-03-15 02:03:43', '0');
INSERT INTO `t_user` VALUES ('17', 'Elaine', '0594-5398478', '1', '3', '2017-03-08 15:03:03', '1');
INSERT INTO `t_user` VALUES ('18', 'Robert', '0594-5397830', '0', '5', '2016-02-10 22:02:06', '0');
INSERT INTO `t_user` VALUES ('19', 'Patrick', '0594-5396516', '0', '4', '2015-09-10 07:09:51', '0');
INSERT INTO `t_user` VALUES ('20', 'Darrel', '0594-5397417', '0', '1', '2016-03-11 11:03:36', '0');
INSERT INTO `t_user` VALUES ('21', 'Salvador', '0594-5399732', '1', '3', '2016-01-01 15:01:21', '0');
INSERT INTO `t_user` VALUES ('22', 'Brandon', '0594-5396204', '1', '4', '2016-05-12 06:05:40', '1');
INSERT INTO `t_user` VALUES ('23', 'Dorothy', '0594-5396783', '0', '1', '2016-12-12 10:12:59', '1');
INSERT INTO `t_user` VALUES ('24', 'Kevyn', '0594-5398240', '0', '2', '2016-02-07 04:02:14', '1');
INSERT INTO `t_user` VALUES ('25', 'Brody', '0594-5398774', '1', '1', '2016-12-11 20:12:36', '0');
what cause this different... my English is poor,LOL...
MySQL is too liberal when it comes to data integrity and its default settings often allow you to accomplish data loss without a complaint (see reference for ONLY_FULL_GROUP_BY SQL mode). For instance, it allows incomplete GROUP BY clauses like yours and just picks an arbitrary (not even random) result.
You need to rewrite your query so all columns involved fit into one category:
Be part of an aggregate expression in the SELECT clause:
SELECT MAX(foo) AS maximum_foo
Be part of the GROUP BY clause:
SELECT foo
...
GROUP BY foo
This way you are being specific enough so the result-set is deterministic. Your current code is basically: "Give me one user of each type"—but you don't specify any criteria to pick users. Other DBMSs (Oracle, SQL Server) will complaint: "You didn't say how to choose users". MySQL will merely pick an arbitrary user (but not even a random user, because randomness would imply that chosen users follow a rule, which is not the case). The ONLY_FULL_GROUP_BY SQL mode is just a way to make MySQL behave like other engines.
You can also get rid of your subquery, it really serves no purpose.
No order on outer SELECT causes the difference. However what do you want to achieve? Take only one (which one) from each group (type) or what?
SELECT *
FROM t_user
WHERE id IN (SELECT MAX(id) FROM t_user GROUP BY type)
ORDER BY type
SQL fiddle link

get all the other user names where user=1 exists

I have the following "tblA" and "tblB" table:
CREATE TABLE if not exists tblA
(
id int(11) NOT NULL auto_increment,
sender varchar(255),
receiver varchar(255),
msg varchar(255),
date timestamp,
PRIMARY KEY (id)
);
CREATE TABLE if not exists tblB
(
id int(11) NOT NULL auto_increment,
sno varchar(255),
name varchar(255),
PRIMARY KEY (id)
);
INSERT INTO tblA (sender, receiver, msg,date) VALUES
('1', '2', 'buzz ...','2011-08-21 14:11:09'),
('1', '2', 'test ...','2011-08-21 14:12:19'),
('1', '2', 'check ...','2011-08-21 14:13:29'),
('1', '1', 'test2 ...','2011-08-21 14:14:09'),
('2', '1', 'check2 ...','2011-08-21 14:15:09'),
('2', '1', 'test3 ...','2011-08-21 14:16:09'),
('1', '2', 'buzz ...','2011-08-21 14:17:09'),
('1', '2', 'test ...','2011-08-21 14:18:19'),
('1', '2', 'check ...','2011-08-21 14:19:29'),
('1', '1', 'test2 ...','2011-08-21 14:10:09'),
('3', '1', 'check2 ...','2011-08-21 14:21:09'),
('3', '1', 'test3 ...','2011-08-21 14:22:09'),
('3', '2', 'buzz ...','2011-08-21 14:24:09'),
('3', '2', 'test ...','2011-08-21 14:25:19'),
('1', '3', 'check ...','2011-08-21 14:26:29'),
('1', '3', 'test2 ...','2011-08-21 14:27:09'),
('2', '3', 'check2 ...','2011-08-21 14:28:09'),
('2', '3', 'test3 ...','2011-08-21 14:29:09'),
('1', '2', 'check3 ...','2011-08-21 14:23:09'),
('1', '4', 'test2 ...','2011-08-21 14:27:09'),
('1', '5', 'test2 ...','2011-08-21 14:27:09'),
('2', '6', 'check2 ...','2011-08-21 14:28:09'),
('2', '7', 'test3 ...','2011-08-21 14:29:09'),
('8', '2', 'check3 ...','2011-08-21 14:23:09');
INSERT INTO tblB (sno, name) VALUES
('1', 'Aa'),
('2', 'Bb'),
('3', 'Cc'),
('4', 'Dd'),
('5', 'Ee'),
('6', 'Ff'),
('7', 'Gg'),
('8', 'Hh');
How to get all rows with user : 1existing along with the name of sender/receiver which is not1 (ie:Aa)
I think this will get the unique list:
select distinct b.name
from tblA a join
tblB b
on b.id in (a.sender, a.receiver) and
((b.id <> 1 and (a.sender = 1 or a.receiver = 1)) or
(a.sender = 1 and a.receiver = 1)
);
SELECT ta.*, tb.name
FROM tablA ta, tablB tb WHERE (ta.sender = tb.sno OR ta.receiver = tb.sno)
AND tb.sno <> 1