how can i count how many people in table a? - mysql

how can i count how many people in table a?
there 4 people exists in the below table , how can i get "4" in mysql?
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `a`
-- ----------------------------
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
`products_id` int(11) NOT NULL auto_increment,
`name` varchar(30) default NULL,
`date` datetime default NULL,
PRIMARY KEY (`products_id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of a
-- ----------------------------
INSERT INTO `a` VALUES ('1', 'jimmy', '2014-02-11 09:24:42');
INSERT INTO `a` VALUES ('7', 'khon', '2014-02-19 09:24:50');
INSERT INTO `a` VALUES ('3', 'jimmy', '2014-01-11 09:25:03');
INSERT INTO `a` VALUES ('4', 'kelvin', '2013-12-11 09:25:09');
INSERT INTO `a` VALUES ('5', 'ricky', '2014-02-12 09:25:14');

Use count on distinct name of the persons.
select count(distinct name) from a mytable

select count(distinct(name)) as count from a;

You can use COUNT LIke
SELECT COUNT(DISTINCT name) FROM a;

You are unclear in your question. What is it that you want to count?, number of distinct persons or the total number of people in the table?
SELECT COUNT(name) from a;
You may use DISTINCT clause with it or you can also perform GROUP BY to get the count of users by grouping them.

Related

Add records randomly between select result

I want to add records randomly in select result like this example:
1 a
2 a
- b
3 a
- b
4 a
5 a
6 a
- b
7 a
8 a
- b
In a simple query I select from a table but in this case I want result mix with b table without random a result otherwise I simply could union them and then order random().
EDIT:
-- ----------------------------
-- Table structure for t1
-- ----------------------------
DROP TABLE IF EXISTS `t1`;
CREATE TABLE `t1` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
INSERT INTO `t1` VALUES ('1', 'a');
INSERT INTO `t1` VALUES ('2', 'a');
INSERT INTO `t1` VALUES ('3', 'a');
INSERT INTO `t1` VALUES ('4', 'a');
INSERT INTO `t1` VALUES ('5', 'a');
INSERT INTO `t1` VALUES ('6', 'a');
INSERT INTO `t1` VALUES ('7', 'a');
INSERT INTO `t1` VALUES ('8', 'a');
INSERT INTO `t1` VALUES ('9', 'a');
INSERT INTO `t1` VALUES ('10', 'a');
-- ----------------------------
-- Table structure for t2
-- ----------------------------
DROP TABLE IF EXISTS `t2`;
CREATE TABLE `t2` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `t2` VALUES ('1', 'b');
INSERT INTO `t2` VALUES ('2', 'b');
INSERT INTO `t2` VALUES ('3', 'b');
So I can union two tables and then order by random and the result will be like this:
SELECT
t1.ID,
t1.name
FROM
t1
UNION ALL
SELECT
t2.ID,
t2.name
FROM
t2
ORDER BY RAND();
5 a
2 a
6 a
3 a
7 a
2 b
9 a
1 a
10 a
3 b
4 a
8 a
1 b
But I don't want to random records I just only want mix two tables records like first example.
I hope my question is now clear.
I don't think pure SQL is the best solution here, I would do it in some end framework (I don't know what you use), like PHP, Delphi, Java, etc. This way is more easier and you can control it better. But if you would like to do it in SQL, here an idea:
SET #max_t1 = (SELECT MAX(ID) FROM t1);
SET #max_t2 = (SELECT MAX(ID) FROM t2);
SET #last_order = RAND()*#max_t1/#max_t2;
SELECT
t1.ID, t1.name, t1.ID AS "Ord"
FROM
t1
UNION ALL
SELECT
t2.ID, t2.name, #last_order:=#last_order+RAND()*#max_t1/#max_t2
FROM
t2
ORDER BY Ord
This way we generate a random position for TableB, then in every row we add some random value to it, in according to the size differency between the two tables, then order the whole result on this column.

how to get records count of this month?

how to get records count of this month?
I want to count user of this month ? "this month" should be calculate by mysql itself. how to write the sql?
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `a`
-- ----------------------------
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(30) default NULL,
`date` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of a
-- ----------------------------
INSERT INTO `a` VALUES ('1', 'jimy', '2014-02-11 09:24:42');
INSERT INTO `a` VALUES ('7', 'khon', '2014-02-19 09:24:50');
INSERT INTO `a` VALUES ('3', 'tina', '2014-01-11 09:25:03');
INSERT INTO `a` VALUES ('4', 'kelvin', '2013-12-11 09:25:09');
INSERT INTO `a` VALUES ('5', 'ricky', '2014-02-12 09:25:14');
you can try this
Select count(*)
FROM a
WHERE MONTH(date) = MONTH(NOW())
GROUP BY MONTH(date)
MONTH(date) only returns the month number, it won't work with multiple years data.
SELECT COUNT(*) count
FROM a
WHERE EXTRACT(YEAR_MONTH FROM a.date) = EXTRACT(YEAR_MONTH FROM NOW())
Or you could use:
SELECT COUNT(*) count
FROM a
WHERE DATE_FORMAT(a.date,'%Y%m') = DATE_FORMAT(NOW(),'%Y%m')

how to get average time 'xx:xx:xx' on mysql?

please see the the test data bellow. I want to get the avgtime (=timeonsite/visits) and display as "xx:xx:xx" result in mysql. how can I get it?
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `t`
-- ----------------------------
DROP TABLE IF EXISTS `t`;
CREATE TABLE `t` (
`id` int(11) NOT NULL auto_increment,
`timeOnsite` time default NULL,
`visits` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of t
-- ----------------------------
INSERT INTO `t` VALUES ('1', '04:05:30', '20');
INSERT INTO `t` VALUES ('2', '03:00:00', '10');
INSERT INTO `t` VALUES ('3', '00:01:30', '17');
You can use TIME_TO_SEC function to change xx:xx:xx format to seconds.
SELECT TIME_TO_SEC('00:01:30') / 17; # return 5.2941
And then through SEC_TO_TIME you can convert seconds to time back as below :
SELECT SEC_TO_TIME(TIME_TO_SEC('00:01:30') / 17); # return 00:00:05
Are sure that you calculate avgtime in such way?
If yes, mysql select below:
select id, timeOnsite,visits, SEC_TO_TIME(TIME_TO_SEC(timeOnsite)/visits) as avgtime
from t

How implementation my wants with MySQL JOIN

_
Hello everyone!
I have table
CREATE TABLE `labels` (
`id` INT NULL AUTO_INCREMENT DEFAULT NULL,
`name` VARCHAR(250) NULL DEFAULT NULL,
`score` INT NULL DEFAULT NULL,
`before_score` INT NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);
And I Have This Table
CREATE TABLE `scores` (
`id` INT NULL AUTO_INCREMENT DEFAULT NULL,
`name_id` INT NULL DEFAULT NULL,
`score` INT NULL DEFAULT NULL,
`date` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`)
);
And i want have result where labels.score - have value last scores.score sorted by scores.date and labels.before_score where have value penultimate scores.score sorted by scores.date. Can I do This Only on Mysql slq and how?
Thanks.
ADD
For example i have this data on first table:
INSERT INTO `labels` (id, name, score, before_score) VALUES (1, 'John', 200, 123);
INSERT INTO `labels` (id, name, score, before_score) VALUES (2, 'Eddie', 2000, 2000);
INSERT INTO `labels` (id, name, score, before_score) VALUES (3, 'Bob', 400, 3101);
And second table
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('1','1','12','2013-07-10');
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('2','2','2000','2013-05-04');
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('3','3','654','2012-09-12');
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('4','1','123','2013-12-17');
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('5','1','200','2014-04-25');
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('6','3','3101','2013-12-02');
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('6','2','2000','2015-12-02');
INSERT INTO `scores` (`id`,`name_id`,`score`,`date`) VALUES ('6','3','400','2013-12-02');
If I understand correctly, you need the last two scores for each name_id.
I would tackle this with temporary tables:
Step 1. Last score:
create temporary table temp_score1
select name_id, max(`date`) as lastDate
from scores
group by name_id;
-- Add the appropriate indexes
alter table temp_score1
add unique index idx_name_id(name_id),
add index idx_lastDate(lastDate);
Step 2. Penultimate score. The idea is exactly the same, but using temp_score1 to filter the data:
create temporary table temp_score2
select s.name_id, max(`date`) as penultimateDate
from scores as s
inner join temp_score1 as t on s.nameId = t.nameId
where s.`date` < t.lastDate
group by name_id;
-- Add the appropriate indexes
alter table temp_score2
add unique index idx_name_id(name_id),
add index idx_penultimateDate(penultimateDate);
Step 3. Put it all together.
select
l.id, l.name,
s1.lastScore, s2.penultimateScore
from
`labels` as l
left join temp_score1 as s1 on l.id = s1.name_id
left join temp_score2 as s2 on l.id = s2.name_id
You can put this three steps inside a stored procedure.
Hope this helps you.

Mysql Temporary Table Syntax

I have a question about the syntax for creating and accessing temporary tables.Here is a
related question.
My table
CREATE TABLE IF NOT EXISTS `table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `table1`
--
INSERT INTO `table1` (`id`, `name`, `address`) VALUES
(1, 'andrew', '5 road'),
(2, 'bob', '6 street');
I am running this query.
CREATE TEMPORARY TABLE temptable SELECT id, name, address
FROM table1
SELECT id, name, address
FROM temptable
And tried this one
CREATE TEMPORARY TABLE temptable SELECT id, name, address
FROM table1
DESCRIBE temptable
Creating the temp table works, but then when I try to get info out of the temp table I get a message saying I need to check my sql syntax.
thanks
andrew
I left out the ';' after each statement. My query should have looked like this
CREATE TEMPORARY TABLE temptable SELECT id, name, address
FROM table1;
SELECT id, name, address
FROM temptable;
Details are important in programming and so is stackoverflow