I am having table member(id, name , email, altEmail, phone, altPhone, date).
id is unique 1,2,3,4,5,6,7.....520,521.
id email altemail
1 2#h.v 2#T.h
2 3#gy.c -
3 - 4#t.c
I need output as :
id mail
1 2#h.v
1 2#T.h
2 3#gy.c
3 4#t.c
You can use UNION ALL
SELECT `id`, `email` mail FROM t WHERE `email` <> '-'
UNION ALL
SELECT `id`, `altemail` mail FROM t WHERE `altemail` <> '-'
ORDER BY `id`
Demo
Edit for null values
SELECT `id`, `email` mail FROM t WHERE `email` <> '' AND `email` IS NOT NULL
UNION ALL
SELECT `id`, `altemail` mail FROM t WHERE `altemail` <> '' AND `altemail` IS NOT NULL
ORDER BY `id`
Demo
Try this ...
select id, email as mail from member union select id, altemail from member order by id;
Related
How can i calculate how many unique (hit_type = click) per campaign in the following scenario? :
CREATE TABLE statistics
(`id` int, `uid` int, `hit_type` varchar(10), `name` varchar(55), `date` date)
;
INSERT INTO statistics
(`id`, `uid`, `hit_type`, `name`, `date`)
VALUES
(1,'100','visit','campaign1','2015-03-18'),
(2,'100','visit','campaign1','2015-03-19'),
(3,'100','click','campaign1','2015-03-18'),
(4,'100','click','campaign1','2015-03-18'),
(5,'100','click','campaign1','2015-03-20'),
(6,'100','client','campaign1','2015-03-19'),
(7,'100','client','campaign1','2015-03-20'),
(8,'200','visit','campaign1','2015-03-19'),
(9,'200','visit','campaign1','2015-03-19'),
(10,'200','visit','campaign2','2015-03-20'),
(11,'200','click','campaign1','2015-03-18'),
(12,'200','click','campaign1','2015-03-19'),
(13,'200','client','campaign1','2015-03-19'),
(14,'200','client','campaign2','2015-03-20')
;
SELECT `name`,
SUM(IF(`hit_type` = 'click',1,0)) as click ,
SUM(IF(`hit_type` = 'visit',1,0)) as visit,
SUM(IF(`hit_type` = 'client',1,0)) as client
FROM `statistics`
WHERE `name` IN('campaign1','campaign2')
GROUP BY `name`
ORDER BY `name`
If you are looking for unique dates, then you can do:
SELECT `name`,
COUNT(DISTINCT CASE WHEN `hit_type` = 'click' THEN date END) as click ,
COUNT(DISTINCT CASE WHEN `hit_type` = 'visit' THEN date END) as visit,
COUNT(DISTINCT CASE WHEN `hit_type` = 'client' THEN date END) as client
FROM `statistics`
WHERE `name` IN('campaign1','campaign2')
GROUP BY `name`
ORDER BY `name`;
My Table Structure is
CREATE TABLE IF NOT EXISTS `tbl_user_result` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`ResultId`int(11),
`PaperId` int(11) ,
`Title` varchar(45),
`TotalQuestions` int(11) ,
`Attempt` int(11) ,
`Correct` int(11) ,
`Wrong` int(11) ,
`Score` int(45) ,
`Minutes` int(11) ,
`TimeSt` varchar(45) ,
`UserEmail` varchar(45) ,
`UserName` varchar(45) ,
PRIMARY KEY (`Id`));
I tried following SQL query
SELECT
id,
PaperId,
Title,
Score,
Minutes,
(SELECT
COUNT(*) + 1
FROM
tbl_user_result
WHERE
Score > x.Score AND Minutes > x.Minutes) AS Rank
FROM
`tbl_user_result` as x
The easiest way is to use variables:
select r.*
from (select r.*, (#rn := #rn + 1) as rank
from tbl_user_result r cross join (select #rn := 0) vars
order by score desc, minutes asc
) r;
This works if you have no duplicates.
I think the following is the version of your query that you want:
SELECT id, PaperId, Title, Score, Minutes,
(SELECT COUNT(*) + 1
FROM tbl_user_result r2
WHERE r2.Score > r.Score OR
(r2.Score = r.Score AND r2.Minutes < r.Minutes)
) AS Rank
FROM tbl_user_result r;
I have the query below that uses union.
"SELECT * FROM (
SELECT 1 AS `table`,
`comment_post_id` AS `feed_id`,
`blog_id` AS `from_blog`,
`comment_author` AS `author`,
`comment_content_stripped` AS `feed_title`,
`comment_content` AS `post_content_s`,
`type` AS `type`,
null AS `object_type`,
`comment_date_gmt` AS `date`
FROM `wp_site_comments`
UNION
SELECT 2 AS `table`,
`post_id` AS `feed_id`,
null AS `from_blog`,
`blog_id` AS `author`,
`post_title` AS `feed_title`,
`post_content_stripped` AS `post_content_s`,
`post_type` AS `type`,
null AS `object_type`,
`post_published_gmt` AS `date`
FROM `wp_site_posts`
UNION
SELECT 3 AS `table`,
`object_id` AS `feed_id`,
`blog_id` AS `from_blog`,
`user_id` AS `author`,
null AS `feed_title`,
null AS `post_content_s`,
`type` AS `type`,
`object_type` AS `object_type`,
`date_added` AS `date`
FROM `wp_global_likes`
UNION
SELECT 4 AS `table`,
`object_id` AS `feed_id`,
null AS `from_blog`,
`user_id` AS `author`,
null AS `feed_title`,
null AS `post_content_s`,
`type` AS `type`,
`object_type` AS `object_type`,
`date_added` AS `date`
FROM `wp_global_followers`
) AS tb
ORDER BY `date` DESC"
Basically I wanted to select only the rows where author is in a comma separated values as follows:
eg. $blog_ids = (23, 55, 19, 10) and $user_ids = (22, 55, 19, 40)
The first table in union, the author is comment_author which is a user id.
The second table in union, the author isblog_id` which is a blog id.
The third and fourth table in union, the author is user_id which is a user_id.
Now, I wanted to somehow distinct the author as blog id and user id so my query will select rows where author is in $blog_ids and $user_ids uniquely.
I used,
WHERE author in (" . $blog_ids . ") and it returns correct. Now I wanted to include $user_id.
Please note that $blog_ids and $user_ids may have a same value.
I hope you get what I mean, this is i guess the best explanation I can make.
I have this table:
title1 title2 type
---------------------
qwe1 xcv2 3
asd1 tzu 7
fgh1 (empty) 4
and I have this query:
SELECT `title1` AS `title`, `type`
FROM `table`
UNION
SELECT `title2` AS `title`, `type`
FROM `table`
ORDER BY `title` ASC, type
(please see fiddle here).
How can I transform this query so that it doesn't display any rows if title2 is empty? Row 1 shouldn't be displayed.
I tried to implement an IF-statement, but I always get a mysql error.
This should cover the empty string and null:
SELECT `title1` AS `title`, `type`
FROM `table`
UNION
SELECT `title2` AS `title`, `type`
FROM `table` where title2 is not null and title2 <> ''
ORDER BY `title` ASC, type
You should use WHERE statement to define condition for your query
SELECT `title1` AS `title`, `type`
FROM `table`
WHERE `title1` IS NOT NULL AND LENGTH(`title1`) > 0
UNION
SELECT `title2` AS `title`, `type`
FROM `table`
WHERE `title2` IS NOT NULL AND LENGTH(`title2`) > 0
ORDER BY `title` ASC, type
When you use UNION, you remove duplicates. Are you sure you don't want to use UNION ALL? In addition, is there any reason that you need to have a title2? It doesn't seem normalized to me.
SELECT title1 AS title, `type`
FROM `table`
UNION ALL
SELECT title2 AS title, `type`
FROM `table`
WHERE title2 IS NOT NULL AND title2 <> ''
See the demo
I think that, instead to check if title2 is not null and title2 <> '', the shortest way is to use COALESCE to convert any Null value to empty, and then check if the result of COALESCE is <> '', like this:
SELECT title1 AS title, `type`
FROM `table`
UNION
SELECT title2 AS title, `type`
FROM `table`
WHERE COALESCE(title2, '') <> ''
and if you don't need to remove duplicates, i think it's better to use UNION ALL instead of UNION.
I have a following db table review_vote
review_id | user_id | status | date_added
2 3 good 20130116135259
3 3 normal 20130116145259
4 2 normal 20130116155259
5 2 good 20130116165259
6 2 good 20130116175259
7 1 great 20130116185259
8 3 good 20130117135259
9 3 normal 20130117145259
currently I can get the highest vote by using the following query:
SELECT review_id FROM review_vote GROUP BY review_id HAVING COUNT(*) =
(
SELECT MAX(x.counts) FROM
(
SELECT review_id, COUNT(*) counts FROM review_vote GROUP BY review_id
)x
)
order by date_added desc
but if i need to get highest vote perday how can i modified the code above? Which means if today were 2013-01-16 I need to get highest vote for yesterday 2013-01-15
my attempt to use date_added = CURDATE() is failed.
SQLFIDDLE: http://sqlfiddle.com/#!2/9a2b5/6
Update query:
CREATE TABLE IF NOT EXISTS `review_vote` (
`review_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(100) NOT NULL,
`date_added` varchar(100) NOT NULL,
KEY `review_id` (`review_id`,`user_id`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `review_vote` (`review_id`, `user_id`, `status`, `date_added`) VALUES
(223, 2, 'lame', '20130116195259');
INSERT INTO `review_vote` (`review_id`, `user_id`, `status`, `date_added`) VALUES(222, 2, 'lame', '20130115195259');
Just add this in your where condition:
DATE_FORMAT(date_added, '%Y-%m-%d') = CURDATE()
Updating as per your error
SELECT review_id FROM review_vote AS RV WHERE DATE_FORMAT(RV.date_added, '%Y-%m-%d') = CURDATE()
GROUP BY review_id HAVING COUNT() =
(
SELECT MAX(x.counts) FROM
(
SELECT review_id, COUNT() counts FROM review_vote GROUP BY review_id
)x
)
Please have a look at demo :demo
SELECT review_id
FROM review_vote
WHERE date_added LIKE '20130115%'
GROUP BY review_id
ORDER BY COUNT(*) DESC
LIMIT 1
But really you ought to consider changing your date_added column to an appropriate temporal type, like DATETIME or TIMESTAMP.
Add the following to your WHERE clause:
`date_added` BETWEEN
DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y%m%d')
AND DATE_FORMAT(CURDATE(), '%Y%m%d')
Make sure you have an index on date_added!
Documentation for the date functions is all here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Life would be easier for you if your date_added column was a native date type.