I have inherited a simple table from a client, that stores two addresses (shipping address and business address) in just two fields shipping_address and business_address. There is a field called ship_to with enum('shipping','business'), and a field for a full name. The client wants an output as:
full name: address
Where "address" column should be picked by the ship_to value. If ship_to = shipping then use shipping_address column. If ship_to = business then use business_address column.
For instance:
Joe Smith: Roach Street #10, Rats Village
How I can use CONCAT with IF condition? I'm guessing something like this, which obviusly is just an idea:
SELECT CONCAT( full_name, ':', IF (ship_to=='shipping', shipping_address, business_address ) AS contact FROM `table`
But, what's the right syntax for doing this?
Lacking the closing )
SELECT
CONCAT( full_name, ':', IF (ship_to='shipping', shipping_address, business_address )) as contact
FROM TableName
Related
I have a table which has one column of addresses. I have some 10 - 11 places name.
When i query that table using 'Select * ...', i want to create a new column which matches the values with address fields and store that values into new column of exist else 'Not Found'.
The table has address column as below. I want to extract areas from it such as BTM Layot, Wilson Garden
When i do the select query, the output should be that address field and one more field which will give me the abstract location area from address field. And if any value does not matches the address field then it shoud display as 'Area Nt Specified'
Consider a cross join query (query with no joins but a list of tables in FROM clause) between the larger table of addresses (t1) and smaller table of your 10-11 places (t2) holding BTM Layot, Wilson Garden... values. This will be scalable instead of manually entering/editing places in an IN clause.
Then use a LIKE expression in a WHERE clause to match the places which are a part of the larger address string. However, to return all original address values with matched places use the LEFT JOIN...NOT NULL query with cross join as derived table (sub).
SELECT `maintable`.`address`, IFNULL(sub.`place`, 'Area Nt Specified') As matchplaces
FROM `maintable`
LEFT JOIN
(SELECT t1.ID, t1.address, t2.place
FROM `maintable` As t1,
(SELECT `place` FROM `placestable`) As t2
WHERE t1.address LIKE Concat('%',t2.place,'%')) As sub
ON `maintable`.ID = sub.ID
WHERE `maintable`.ID IS NOT NULL;
If really need to use regular expression, replace the LIKE expression in derived table with below:
WHERE t1.address regexp t2.place
If you have a list of know places, then you can do:
select (case when address regexp '(, BTM Layout|, Bapuji Nagar|, Adugodi)$'
then substring_index(address, ', ', -1)
else 'Not Found'
end)
You can expand the regular expression to include as many places as you like.
Or alternatively, you don't really need a regular expression:
select (case when substring_index(address, ', ', -1) in ('BTM Layout', 'Bapuji Nagar', 'Adugodi', . . .)
then substring_index(address, ', ', -1)
else 'Not Found'
end)
I need to make a mysql query that displays all first names concatenated with the last name in uppercase, with a space between first and last name.
What I've got atm:
SELECT CONCAT_WS(" ", `First_Name`, `Last_Name`) AS Name from tblDetails
I'm new to mysql and I can't figure out where to put UPPER, doesn't seem to work anywhere.
Also I need to do the same as above but add a prefix of Mr or Ms depending on the gender.
you can use case and concat to add prefix based on gender and upper can be applied to both First_Name, Last_Name column values and then use concat_ws to append them with a space in between.
SELECT CONCAT ( (case when gender ='M'
then 'Mr '
else 'Ms '
end
),
CONCAT_WS(" ", UPPER(First_Name), UPPER(Last_Name))
)
AS Name
from tblDetails
schema:
customers(name, mailid, city)
What to find:
Find all customer names consisting of three or more words (for example King George V).
What I tried:
select name from customers
where name like
'%[A-Za-z0-9][A-Za-z0-9]% %[A-Za-z0-9][A-Za-z0-9]% %[A-Za-z0-9][A-Za-z0-9]%'
what is surprising me:
If I am trying for two words (removing the last %[A-Za-z0-9]% from my query), its working fine but its not working for three words :(
MySQL Solution:
If a name has words separated by space character, then,
Try the following:
select name from customers
where ( length( name )
-
length( replace( name, ' ', '' ) ) + 1
) >= 3
In t-sql, the like clause can contain multiple wild card checks - eg:
SELECT * FROM Customers WHERE Name like '% % %'
will return those Names where two spaces are contained.
If you are consistent with the spacing between names, you could use this logic
SELECT LENGTH(name)-LENGTH(REPLACE(name,' ',''))
FROM customers
Or you can try this too if your sql dont have length function ( which is the situation I have when I'm doing an online exercies...) Inspired by answers above
SELECT name FROM customers WHERE (replace(name,' ','*')) LIKE '%*%*%'
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%'
I'm having a bit of trouble with something which I think must be pretty simple.
I have a table containing:
CREATE TABLE cities(
city_id INT PRIMARY KEY,
city_article TEXT,
city_name TEXT
);
So, the city_name is a simple field with for example name like 'PARIS' or 'NEW YORK'.
city_article is mostly empty, except for some cities (like french cities) who need an article in front of them like 'LA ROCHELLE' which is set in the database as
city_article = 'LA'
city_name = 'ROCHELLE'
I'd like to create a new column containing the concatenation of city_article and city_name, separated with a '-' and replace every space with the same '-'.
So if we take something like 'THE NEW YORK' separated in
city_article = 'THE'
city_name = 'NEW YORK'
the third column would be
city_concatenated = 'THE-NEW-YORK'
the trick is that since not all cities have an article, I don't want cities like 'PARIS' to become '-PARIS' after the operation.
Is there such a way to do that easily ?
TL;DR: how do I create a new column containing the result of 2 other columns whith one which can be NULL and the other one which must have every space replaced with '-' without putting a '-' where it's not needed due to the absence of the first field.
I am not sure whether or not you could have an empty string in City_article, but I've assumed this could happen:
SELECT CONCAT(CASE WHEN City_Article = '' OR City_Article IS NULL THEN '' ELSE CONCAT(City_Article, '-') END, REPLACE(city_name, ' ', '-'))
FROM Cities;
In terms of adding this as a column, well I wouldn't. Since MySQL does not allow computed columns I'd recommend creating a view that stores the definition of this column. This ensures the full name is always up to date and an additional column doesn't require updating if the article or the city name are updated:
CREATE VIEW Cities_Ext
AS
SELECT City_ID,
City_Article,
City_Name,
CONCAT(CASE WHEN City_Article = '' OR City_Article IS NULL THEN '' ELSE CONCAT(City_Article, '-') END,REPLACE(city_name, ' ', '-')) AS FullName
FROM Cities;
Example on SQL Fiddle
Put the first dash inside a COALESCE statement:
SELECT COALESCE(city_article + '-','') + REPLACE(city_name, ' ', '-')