I was wondering if you have two columns for lets say
first name.
last name.
what you store in the database.
Can you create a 'dynamic' column 'fullname' in the database that automatically creates the name out of the first and last name?
firstname |lastname |fullname (this goes automatically)
-----------------------------------------
foo |bar |foo bar
If you just want to select the full name, you can do just that:
select concat(firstname, ' ', lastname) as fullname from users
If you actually want a column (i.e. be able to perform updates against it), then it's not possible, AFAIK.
You can try something like this:
SELECT CONCAT(firstname, ' ', lastname) as firstlast FROM users
This one also makes sure that no empty spaces are added:
SELECT IF(ISNULL(firstname), lastname, CONCAT(firstname, ' ', lastname)) AS fullname FROM users
Related
I'm in a bit of a time crunch. I have a table with 2 columns. firstName, and lastName.
The data imported into SQL had both first and last name inside of the firstName column.
Is there a quick way to update the table to put everything before the first space into the lastName, and everything after the space into the firstName column? I know I could export, and do this via excel, but it in close to production time so I would rather not risk issues.
I have looked at a couple different split posts on here, but those do not involve updating at the same time.
Try to use:
UPDATE
table
SET
lastname = SUBSTRING(firstName, 1, CHARINDEX(' ', firstName) - 1),
firstName= SUBSTRING(firstName, CHARINDEX(' ', firstName) + 1, LEN(firstName))
MySQL
Demonstration:
SET #str := 'Robert Optional Dickey';
SELECT
SUBSTRING_INDEX(#str,SUBSTRING_INDEX(#str,' ',-1),1) AS lastName,
SUBSTRING_INDEX(#str,' ',-1) AS firstName;
Output:
lastName firstName
Robert Optional Dickey
Update Query:
UPDATE your_table
SET lastName = SUBSTRING_INDEX(#str,SUBSTRING_INDEX(#str,' ',-1),1),
firstName = SUBSTRING_INDEX(#str,' ',-1);
Note: It will work for any number spaces inside the full name.It just considers the string after the last space as first name.
I have a NAME column and the data is stored like this in a MySql database:
Doe,John P
I would like to break it out into FNAME LNAME MNAME and FULLNAME.
If I don't want to tamper with the SQL table, should I create a view?
I know how to fix it in PHP with a function but I would rather the processing be done on the database server than the application server.
This is what I have so far. I thinking I need a compound query or something similar.
SELECT Name
, SUBSTRING_INDEX(Name,',',-1) As FNAME
, SUBSTRING_INDEX(Name,',',1) As LNAME
, SUBSTRING_INDEX(Name,' ',1) As MNAME
FROM people;
I'm having trouble grabbing the middle name and doing what I think should be CONCAT function to get FULLNAME.
It would be great if there was a way (maybe a conditional) to handle names in the database that didn't have middle names. (i.e. DOE,JOHN).
A view is a fine way to get the results you want for future queries. The defining query would be:
SELECT Name, SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ',', -1), ' ', 1) As FNAME,
SUBSTRING_INDEX(Name, ',', 1) As LNAME,
SUBSTRING_INDEX(Name, ' ', -1) As MNAME
FROM people;
This will work assuming that the format is exactly correct as you describe it in the table. Small changes -- such as no middle name or a space after the comma -- would make the syntax more complicated.
So the search would be like 'Ozzie Smith'
First table has (username, fname, lname) = osmith, ozzie, smith
Second table has (username, team) = osmith, cardinals
I need to concat the first and last name columns from the first table join it by username on the second table and return the team name.
I have been trying and trying...brain melted. I need your help!
Thanks!
Since it's MySQL, you need to use CONCAT, not the + sign. I also added a LOWER() function to avoid capital letter mismatch problem :
select team
from table1
join table2 on table2.username = table1.username
where lower(concat(fname,' ',lname)) like lower('%Ozzie Smith%')
You're probably doing something like
WHERE fname LIKE '%Ozzie Smith%'
which will not work. fname will contain only Ozzie, and will never match against Ozzie Smith. It would also not match if the user enters Smith, Ozzie, or any other dual-name variant. You'll have to preprocess the search terms and do something like
WHERE (fname LIKE '%Ozzie%') or (fname LIKE '%Smith%')
OR (lname LIKE '%ozzie%') or (lname LIKE %Smith%')
This will get VERY painful as the number of search terms and fields being search goes up. You'd be better off using a FULLTEXT index, where it'll be a simple matter of
WHERE MATCH(fname, lname) AGAINST ('Ozzie Smith')
Why doesn't this work?
select
lname + ', ' + fname as playerName
, team as teamName
from table1
join table2 on table2.username = table1.username
Update:
where (fname + ' ' + lname) like '%Ozzie Smith%'
For a search function, I need to be able to get the mysql function to show up results by combining two columns.
To be more precise: I have a table in the database called "members" in that there is a column called "firstname" and one called "lastname" which I need to use for this. When someone types in a whole name, eg. Jane Doe, I want the query to look whether combining firstname + a space( ?) + lastname brings forth any results.
My code so far is this:
$poster = mysql_query("SELECT id FROM members WHERE (firstname' 'lastname) LIKE '%$search%'") or die(mysql_error());
$poster = mysql_fetch_object($poster);
I know that's probably wrong, it's the most recent I've tried after trying with brackets around the firstname + lastname bits, etc, etc... But yes, any help would be greatly appreciated.
Use the CONCAT function:
SELECT id FROM members
WHERE CONCAT(firstname, ' ', lastname) LIKE ?
Try CONCAT
SELECT id FROM members WHERE concat(firstname, ' ', lastname) LIKE '%$search%'
SELECT id FROM members WHERE (firstname + ' ' + lastname) LIKE '%$search%'
I believe what you are looking for is:
$name_var = 'John Doe';
// Example 1 - using concatenation
$sql = "SELECT id FROM members WHERE CONCAT(firstname, ' ', lastname) = '$name_var'";
The above statement will search for everything where the first name is John and the last name is Doe
This is rather ineficcient as it will have to evaluate the CONCAT everytime in mysql I believe
I would reccomend validating in PHP that the string is two words as you expect e.g.
$name_var = 'John Doe';
// this will split the string based on spaces
$names = explode(' ', $name_var);
$first_name = $names[0];
$last_name = $names[1];
// Example 2 - searching each field
$sql = "SELECT id FROM members WHERE firstname = '$first_name' AND lastname = '$last_name'";
The above statement will still search for everything where the first name is John and the last name is Doe
In the above statment you are actually just searching based on the exact values so it is much more efficient. If this query is going to be ran regularly, you should also add indexes to the firstname and lastname fields in your mysql table as it will greatly increase the speed!
hope this helps!
Tom
Consider I am having tow fields in my table
field_profile_first_name_value field_profile_last_name_value
Victor Brecher
Abhi Jass
select field_profile_first_name_value, field_profile_last_name_value
from content_type_profile where field_profile_first_name_value LIKE '%Victor Bre%' OR
field_profile_last_name_value LIKE '%Victor Bre%'
I am having a auto complete text box.
When i enter the keyword as victor it will fetch the result. But if i give the first name and last name in the same time it will not produce the result.
That is if i give the keyword as Victor Brecher it will not produce the result.
How can i make to get the result if i enter first name and last name ?
How can i make it ?
Try :
select * from contracts
where lower(concat_ws(' ', field_profile_first_name_value, field_profile_last_name_value))
like lower('%Victor Bre%')
Well, even you don't need to use lower too. just use it simply.
select * from contracts where concat_ws(' ', field_profile_first_name_value,field_profile_last_name_value)
like '%Victor Bre%'
I would make use of soundex.
select * from contracts where SOUNDEX(concat_ws(' ', field_profile_first_name_value,field_profile_last_name_value)) = SOUNDEX('Victor Bre');
I suggest to use this code, so the search works also if the search input is the lastname before the name
SELECT
*
FROM
contracts
WHERE
CONCAT(firstname, ' ', lastname)) LIKE 'Victor Bre%'
OR CONCAT(lastname, ' ', firstname)) LIKE 'Victor Bre%';
Moreover, I tested this sql code with and without LOWER(), in mysql 5.7+ the search already do that.