MySQL get all characters before specific character - mysql

I have a table where I extract some values, one column values can contain "value1|value2|value3", but I only want to get the characters before the | - "value1".
This is what I tried, but it doesn't work.. What am I doing wrong?
Thanks!
$sql = "SELECT * LEFT('Like', LOCATE('|', 'Like')-1) FROM $tablename
WHERE Parent = '0' AND Type LIKE 'top' ORDER BY Order ASC";
I want to use this for ALL values, not just one field..

you need the following statement to get that portion of [ColName]:
LEFT([ColName],INSTR([ColName],"|")-1)
If you want to select multiple columns into the same recordset column you can union all with something like the following:
SELECT LEFT(ColName,INSTR(ColName,"|")-1) AS FirstValue From $TableName;
UNION ALL
SELECT LEFT(ColName2,INSTR(ColName2,"|")-1) AS FirstValue From $TableName;
If you want to use this on multiple columns, script the creation of the sql.

Two things: (1) you don't have a comma between your * and the expression you're trying to do with LEFT and (2) you're putting like in quotes, so the functions are working on the constant value like instead of your column named like. Try putting like in backticks.
SELECT *, LEFT(`Like`, LOCATE('|', `Like`)-1)
...
You can also use the MySQL SUBSTRING_INDEX function for this:
SELECT *, SUBSTRING_INDEX(`Like`, '|', 1)
...

Related

select data mysql

i have in my table places named field. there are space separated values(there are problem to store csv value in one field). now i want to fire query like below. how i can do ??
select * from tablename where variablename in places
i did try this way but it shows syntax error.
select * from tablename where variablename in replace(places,' ',',')
### places ###
bank finance point_of_interest establishment
Use FIND_IN_SET
For comma separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', variablename ) )
Refer : SQL Fiddle
For space separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', replace(variablename,' ',',') ) )
Refer : SQL Fiddle
The best solution would be to normalise your data structure and do not have a single field storing multiple values.
You can make a query work without normalisation, but any solutions would be lot less optimal from a performance point of view.
Use patter matching with like operator:
... where fieldname like '% searched_value %'
Use the replace() function and combine it with find_in_set():
... where find_in_set('searched_value',replace(fieldname,' ',','))>0
Hi I think your problem comes from the usage of IN
IN for MySql is used like this
SELECT *
FROM table_name
WHERE column_name IN (bank,finance,point_of_interest, establishment);
In case of you want to select places you need to specify each place into value like

Mysql - how to use like on multiple columns

I've searched in mysql query a word in multiple column in java program. The number of column is variable.
It is correct this query:
select * from customer with (city, name) like%'adelaide'%
You can use CONCAT() function:
select * from customer WHERE concat(city,name) like '%adelaide%'
You can add as many columns to the concat function as you like.
Also as you see I changed your like syntax, '%WORD%' and used a simple where clause.
It's better to use CONCAT_WS() function.
CONCAT() returns NULL if any argument is NULL.
CONCAT_WS() skip any NULL values after the separator argument.
multiple like statements can not be used with or directly. You have to use column name for each like statement.
Use multiple like as mentioned below.
Select *
from animals
where
(
[animalscolumn] like ('%dog%') or
[animalscolumn] like ('%cat%') or
[animalscolumn] like ('%gerbil%') or
[animalscolumn] like ('%hamster%') or
[animalscolumn] like ('%guinea pig%')
)
select * from customer with city like%'adelaide'% or name like%'adelaide'%

How do I select columns which has a dot in their column names

I am trying to select a record which clearly exists, but my SQL query does not bring it up. Any idea how to get this working?
SELECT * FROM Users WHERE 'local.email'='burgundy#email.com' LIMIT 1
The issue is that you're using single quotes ( ' ) around your column name, rather than using backticks ( ` ).
Try using this instead:
SELECT *
FROM Users
WHERE `local.email` = 'burgundy#email.com'
LIMIT 1
Like Crocodile said, anything that is a SQL variable like a table name or column name can also be surrounded by `` Back ticks (hold shift and hit ~). This tells SQL to look at them as literals.

Selecting just one column instead of all columns in a mysql query

I am writing a PHP script where I will search for a keyword in a specific table and return the id of that keyword.
What I have done so far is:
SELECT * FROM 'table' WHERE column_name LIKE '%keyword%'
This returns the entire row of the keyword. What I want is a query that will just return the id of that row, so I can put it in a variable.
First, you should probably replace the quotes around 'table' with backticks, or you'll likely get syntax errors somewhere. The rest of my query examples use backticks for the table name. It's advised to use them for column names too to reduce the chance of conflicts.
Instead of using * to fetch all columns, explicitly list which columns you want to fetch:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'
The * wildcard selects all columns as you have noticed from your original query.
If you want to select other columns, list them as comma separated names:
SELECT id, foo, bar FROM `table` WHERE column_name LIKE '%keyword%'
Since you are using mySQL:
SELECT id FROM `table` WHERE column_name LIKE '%keyword%'

MySql Select rows where value IN comma-delimited column

I'm trying do something like this:
SELECT * FROM table WHERE column IN (1,2,3)
but in place of 1,2,3 I want to use a column from another table that contains a comma-delimited list just like "1,2,3" above.
I have tried to do this:
SELECT
GROUP_CONCAT(a.eating_area SEPARATOR ', ')
FROM table_areas a
WHERE a.eating_area_id IN (
SELECT
o.eating_area_ids
FROM table_offers o WHERE o.rid=1
)
however this only returns the value associated with 1, and not 2 or 3. Can this be done or is there another way to do this?
Many thanks
SELECT * FROM table t
WHERE IF(FIND_IN_SET(column,(SELECT "1,2,3" FROM otherTable WHERE 1))>=1,1,0)
-- FIND_IN_SET will return the position.
I don't know if it's the best way to do it but... i think it could work.
Source: Find_in_set