mysql find out the non repeated customers - mysql

I have a table like this,
test#text.com
test12#text.com
test123#text.com
test12#text.com
test#text.com
test12#text.com
test1#text.com
I want the email which are not repeated such as result will be
test1#text.com

Assuming your table name is sample_table your column name is sample_column.
Try this
select sample_column
from sample_table
group by sample_column
having count(sample_column)=1;
DEMO

You can try this...
SELECT DISTINCT(email_column_name)
from tableName
GROUP BY email_column_name;
and if you want more columns information related row then try this.
SELECT DISTINCT(email_column_name), ID, firstName,LastName
from tableName
GROUP BY email_column_name;

This is incorrect:
SELECT DISTINCT(email_column_name), ID, firstName, LastName
DISTINCT works on all columns selected, it is not a function where you can apply it to a single column in the select. So if an email were to appear twice with a different last name that email would be returned in both cases.

Related

Mysql show other different cell data with under group by same name

Let's assume I have a table which store register user data, the records might have same registered name but different email, like following:
I want to create a front view to manipulate those data but I don't want those same name show repeatedly, can mysql statement query to output result like
this is the result so far I can do but it can't bind same name into one.
select * from `register`
where `fullname` in (
select `fullname` from `register`
group by `fullname` having count(*) > 1
)
One thing you could do is to do a SELECT DISTINCT on the duplicate row, and make use of the GROUP_CONCAT(); function in MYSQL to concatenate your desired values into one row, and GROUP BY fullname to get the order you wanted.
Note that I am also putting the user ids into a grouped row, so that you can track which ids belong to which name.
SELECT
DISTINCT fullname as full_name,
GROUP_CONCAT(id SEPARATOR ', ') as user_ids,
GROUP_CONCAT(email SEPARATOR ', ') as emails
FROM
tbl_register
GROUP BY
tbl_register.fullname
Working SQL Fiddle
This would be the logical way to do it. Hope this helped. :)
More information on the GROUP_CONCAT(); function here: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Try this:
SELECT DISTINCT *duplicate_column* FROM *table_name1* WHERE *col_id* IN (SELECT *cols_to_dusplay* FROM *table_name1* GROUP_BY *duplicate_column*

Write a Select Statement to Query

I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.

add column during mysql select

I'd like to add a column to the results of a MySql select statement. For instance, if I have a table:
|id||name||last_name|
Then when I do a select I'd like it to return:
|id||name||last_name||some_value|
where I specify |some_value| at query time. Is this possible?
Try this
SELECT
id, name,last_name, 1 AS some_value
FROM table
Yes, that is possible.
If you want to just hard code a value you can do this:
SELECT id, name, last_name, 'value' AS some_value
FROM table
Or you can aggregate or modify existing data in the table like this:
SELECT id, name, last_name, CONCAT(name, ' ', lastname) AS some_value
FROM table

Masking values in SQL select return

This is an odd question, but I was wonder if you could display one column in the results, but really have another column as the values (MYSQL). Suppose I have this table:
ID Name
1 Soccer
2 Football
I was wonder if it was possible to select all IDs from this table (select ID from table), but the results would display the name instead.
Or is it possible to display as the result (as a single column)?:
1 (Soccer)
2 (Football)
SELECT ID, CONCAT("(", name, ")") FROM <TABLENAME>
This will have results concatenated into single column.
SELECT CONCAT(ID, ' (', Name, ')')
FROM tableName
SQLFiddle Demo

MySQL order question

Is there a way I can order a group of users by first
name and if a user didn't enter a first name
order that user by last name or if they didn't enter a last name order them by middle name?
You could do something like this:
SELECT fields
FROM table
WHERE condition
ORDER BY first_name, last_name, middle_name ASC
I'm assuming 'didn't enter' equates to a null field.
SELECT fields
FROM table
WHERE condition
ORDER BY COALESCE(first_name, last_name, middle_name)