Remove all but first character of a string in MySQL query - mysql

I am trying to run a query directly in MySQL to run through a series of records and replace the value of field with just the field character of the row.
e.g based on a column called formname
row 1 - Renault
row 2 - Citreon
row 3 - Jaguar
That will return..
row 1 - R
row 2 - C
row 3 - J
I can do this easily using native PHP functions but unsure how to do this using a native MYSQL function SUBSTR function?

You can use left
update table
set column = left(column,1)
or for select you can write
select col1,left(col2,1)
from table
Demo

To get this with the SUBSTR() function like you are asking for, you would use:
SELECT Field1, SUBSTR(field2, 1, 1)
FROM MyTable
The function definition: SUBSTR(str,pos,len)

Related

mysql FIND_IN_SET returned an empty result set

I have the following input table "eastate_facilitylist":
eastate_id
facilityList
1
2,3,4
2
1,3,4
3
1
I want to select the "eastate_id" values that have a value inside it, so I've crafted the following query:
SELECT eastate_id FROM `eastate_facilitylist`
WHERE ( FIND_IN_SET(3, facilityList))
Problem is that it returns an empty result set.
I'm using MySQL 5.7.31.
How can I fix my query?

SQL SELECT everthing with value 5 but not specify from which column

I have tried to select something with SQL, and I've a problem with it.
What I want:
SQL SELECT * FROM table WHERE ? = '5';
Select everything which = 5, BUT not specify from which column.
Example:
From this ""database"", you should receive the 1st and the last row.
Is that possible?
You have to list the columns but you can use in. The where clause looks like:
where 5 in (price, height)
Note: This assumes that the columns have the same type. You could get type conversion errors if they are not.
Also, given the names of the column and the data, I assume that the columns are stored as numbers. Hence, I dropped the single quotes around 5. If they are really strings, then use the single quotes.
you need to add a condition to your query with or keyword so if any of them match the row will be shown as a result
SELECT * FROM tablename WHERE price =5 or height= 5
better you list your columns by name instead of using * after SELECT

Select query to select another table based on the result it returns

At the moment I am running a simple select query i.e.
SELECT * FROM `form_expense`
One of the columns in this case titled 'value' returns the following text list_expense_category. I would like to then look up all the values in the table list_expense_category and replace the value with all the results separated by a comma.
I know I am able to do this with an inner join if the value always references the same table but how would I go about this if the value could reference different tables. i.e.
Row 1 - value = list_expense_category
Row 2 - value = list_expense_projects
Row 3 - value = list_expense_currency

how to count (fast) if a value already exists in mysql column?

Currently I have a loop that I use to check each value from array.
foreach k, v = []array{
"SELECT COUNT(*) from column WHERE v='$v'"
}
It works fine except that it's not performant. Is there any way that I can perform this query somehow in batches(e.g. like the IN clause) and get the result in batches too.
Try this to check if the value exists in the table:
SELECT 1 from column WHERE v='$v'
and if you want to check for multiple values then try this:
SELECT 1 from column WHERE v in ('$v',....)

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).