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)
Related
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*
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.
I am trying to search two columns where value is alike and only display the data of the column that has the value not the other column, both columns can not contain the same value.
My code
SELECT CONCAT(`fname`,`lname`)as name ,date
FROM `table` WHERE id='1' AND CONCAT(`fname`,`lname`) LIKE '%james%'
ORDER BY date ASC LIMIT 0,1
The current query outputs both fname and last name value where it finds the %like% i just want it to output only the column where the %like% is found.
Try this:
SELECT
CASE
WHEN fname LIKE '%james%'
THEN fname
ELSE lname
END
AS name
FROM `table`
WHERE id='1' AND CONCAT(`fname`,`lname`) LIKE '%james%'
ORDER BY date ASC LIMIT 0,1
select concat(if(fname, fname, ""), if(lname, lname, "")) as name, date ...
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.
i am trying to sort mysql data with alphabeticaly like
A | B | C | D
when i click on B this query runs
select name from user order by 'b'
but result showing all records starting with a or c or d i want to show records only starting with b
thanks for help
i want to show records only starting with b
select name from user where name LIKE 'b%';
i am trying to sort MySQL data alphabeticaly
select name from user ORDER BY name;
i am trying to sort MySQL data in reverse alphabetic order
select name from user ORDER BY name desc;
but result showing all records
starting with a or c or d i want to
show records only starting with b
You should use WHERE in that case:
select name from user where name = 'b' order by name
If you want to allow regex, you can use the LIKE operator there too if you want. Example:
select name from user where name like 'b%' order by name
That will select records starting with b. Following query on the other hand will select all rows where b is found anywhere in the column:
select name from user where name like '%b%' order by name
You can use:
SELECT name FROM user WHERE name like 'b%' ORDER BY name
If you want to restrict the rows that are returned by a query, you need to use a WHERE clause, rather than an ORDER BY clause. Try
select name from user where name like 'b%'
You do not need to user where clause while ordering the data alphabetically.
here is my code
SELECT * FROM tbl_name ORDER BY field_name
that's it.
It return the data in alphabetical order ie; From A to Z.
:)
I had the same challenge, but after little research I came up with this and it gave me what I wanted, and I was able to overcome that path.
SELECT * from TABLE ORDER BY name
Wildcard Characters are used with like clause to sort records.
If we want to search a string which is starts with B then code is like the following:
select * from tablename where colname like 'B%' order by columnname ;
If we want to search a string which is ends with B then code is like the following:
select * from tablename where colname like '%B' order by columnname ;
If we want to search a string which is contains B then code is like the following:
select * from tablename where colname like '%B%' order by columnname ;
If we want to search a string in which second character is B then code is like the following:
select * from tablename where colname like '_B%' order by columnname ;
If we want to search a string in which third character is B then code is like the following:
select * from tablename where colname like '__B%' order by columnname ;
Note : one underscore for one character.
I try to sort data with query it working fine for me please try this:
select name from user order by name asc
Also try below query for search record by alphabetically
SELECT name FROM `user` WHERE `name` LIKE 'b%'
MySQL solution:
select Name from Employee order by Name ;
Order by will order the names from a to z.