I am looking for a way to run a simple SELECT statment. I have a table which has two columns: id and email.
I want to run a SELECT statment that won't return duplicate values. For example, take the following data:
1 example#hotmail.com
2 example12#hotmail.com
3 example#hotmail.com
4 example#hotmail.com
I want it to return only the following:
2 example12#hotmail.com
Use aggregation count(*) and check the result of aggregate function using having clause to filter out those records which are not duplicated
select *
from demo
group by email
having count(*) = 1
Demo
select id,email from table group by email having count(*) =1;
Related
In my database, I have a table called 'results' with four columns (name,device,passed,failed). Suppose if there are 4 rows in the table as below.
name device passed failed
test1 device_1 2 1
test1 device_2 3 0
test2 device_1 1 2
test2 device_2 2 1
I want the below result:
[(test1,device_1,2,1),(test1,device_2,3,0)]
[(test2,device_1,1,2),(test1,device_2,2,1)]
Is it possible to get that result with only one query to DB?
currently I am querying database twice, first getting a list of distinct names and then getting rows with that name. Below is the pseudo code.
test_names = SELECT DISTINCT name FROM results
for test_name in test_names:
rows = SELECT * FROM results WHERE name=test_name
then I am processing rows to get the object structure that I wanted.
You need concatenation functions like CONCAT_WS() and CONCAT to concatenate the columns of each row and then aggregation with GROUP_CONCAT() for each name:
SELECT CONCAT('[', GROUP_CONCAT(CONCAT('(', CONCAT_WS(',', name, device, passed, failed), ')')), ']') AS result
FROM results
GROUP BY name
See the demo.
SELECT GROUP_CONCAT('(',name,',',device,',',passed,',',failed,')') FROM results group by name;
What i want to get?
Get id,name,email of the people who have sent me friend_requests.
Two tables i have used on the basis of which i want to get the details
signup -> id,name,email,firmname and a few more columns.
friends_requests ->id,userId,sentRequests,receivedRequests,friends,dates
Please refer to the below two images.
friends requests table
Signup table
What I am trying?
SELECT * FROM signup WHERE signup.id IN
(SELECT sentRequests FROM friends_request WHERE friends_request.userId=46)
This query only gives one record, whereas if i use below query it gives 3 records
SELECT * FROM signup WHERE signup.id IN (47,48,49)
I know why second query is giving three records because of the IN clause and three id's
But this query will also give the same result which is 47,48,49
SELECT sentRequests FROM friends_request WHERE friends_request.userId=46
But why isn't the first query giving three records?
When both the values are same? Then why isn't the result same?
About table friends_requests. Column sentRequests violates 1NF.
if I am right, IN operator gets your information as full string, not as separate ID information.
SELECT *
FROM signup
WHERE signup.id IN ('47,48,49')
what is operator thinks IN ('47,48,49')
what You think IN (47,48,49)
You have to split column value '47,48,49' into returning rows so operator could understand, if you want to use IN operator...
For it to work like you coded it,
SELECT sentRequests FROM friends_request WHERE friends_request.userId=46
would have to give 3 rows with 47,48,49.
Because sentRequests is a string it gives one row with 47,48,49
If you want the 3 values go with:
select * from #signup a where exists (select 1 from friends_request b where ','+sentRequests+',' like ',%'+cast(id as varchar(5))+',%')
try this...
SELECT * FROM signup WHERE id IN (
SELECT
DISTINCT cast(SUBSTRING_INDEX(SUBSTRING_INDEX(sentRequests, ',', n.digit+1), ',', -1) AS signed) sentRequests
FROM friends_request INNER JOIN (
SELECT
0 digit
UNION ALL
SELECT 1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5
UNION ALL
SELECT 6
) n ON LENGTH(REPLACE(sentRequests, ',' , '')) <= LENGTH(sentRequests)-n.digit
WHERE userId = 46
);
If I have a MySql table with values
col1
=====
1
1
1
5
5
5
5
7
7
I want to get all distinct values of col1 (i.e. 1,5,) and get output with one SELECT statement as follows:
1~1
5~5
7~7
How can I do this?
Here's the query:
Use separator ~ in GROUP_CONCAT function.
Query #1 use case scenario: If you want to separate the col1 values by tide (~) only if the corresponding value has identical values across the table.
Query #1:
SELECT
GROUP_CONCAT(col1 SEPARATOR '~') output
FROM scotttable
GROUP BY col1;
SQL FIDDLE DEMO
Note: It will work for any number of identical values.
Query #2 use case scenario: Only if you want the distinct col1 values no matter if they have corresponding identical value across the table exists or not.
Query #2:
SELECT
CONCAT(col1,'~',col1) output
FROM scotttable
GROUP BY col1;
FIDDLE OF THIS QUERY
Here is an option using a subquery:
SELECT CONCAT(t.value, '~', t.value)
FROM
(
SELECT DISTINCT col1 AS value
FROM yourTable
) t
Follow the link below for a running demo:
SQLFiddle
I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC
I am looking for a way to run a simple SELECT statment. I have a table which has two columns: id and email.
I want to run a SELECT statment that won't return duplicate values. For example, take the following data:
1 example#hotmail.com
2 example12#hotmail.com
3 example#hotmail.com
4 example#hotmail.com
I want it to return only the following:
1 example#hotmail.com
2 example12#hotmail.com
...and skip the duplicate values.
SELECT MIN(id), email FROM some_table GROUP BY email
SELECT DISTINCT email FROM table
If you don't need ID use
SELECT DISTINCT email FROM `TABLE_NAME`
else If you need the First ID use
SELECT MIN(ID),email FROM `TABLE_NAME` GROUP BY email
There are several ways to accomplish this, one is to use the DISTINCT clause:
SELECT DISTINCT email FROM your_table;
another way is to summarize counts of the values:
SELECT COUNT (*), email from your_table GROUP BY email;
SELECT DISTINCT UNIQUE_FEILD_NAME FROM YOUR_TABLE_NAME