How to replace value of column with MySQL Query - mysql

I try to make some changes to my MySQL database, and I need to change values of this col name "id_lang" which values now is "6" to change to value "2".
This col is found in many tables, it will be great to have a single query which will to this for all DB at once.
by now i found this query,
SELECT REPLACE(yourcolumn,'ValueInTheColumnTobeReplaced', 'NewValue') as replacedColumnName FROM yourtable
PS: I use PHPMYADMIN
but i cant make it work... PLease help me!!!

You have to use an Update query.
But you can not do this in one query for all tables.
Update yourTable set id_lang=2 where id_lang=6

Use this query
UPDATE yourTable SET id_lang=2 WHERE id_lang=6;

Related

SQL select: how to alias only 1 column and leave the rest unaliased [duplicate]

I have a table with many columns let say column1,....,column20. I don't want to scroll everytime to the end of the result table to see the value of column20. In mssql I usually do
SELECT column20, * FROM TABLE but apparently this is not valid in MySQL. Any hints? (I also don't want to select all columns explicitly in the select statement)
You have to give the table name in your query, otherwise mysql complains :
SELECT column20, mytable.* FROM mytable
PS: I have absolutely no idea as to why, because SELECT *, column20 FROM mytable works just fine... Strange things happens sometimes ^^

How can I retrieve the column names from an empty MySQL select query result

Is there a way to retrieve the column names of a query that returns no data?
The result of this query would be empty.
Is there a way how to find the column names when there's no result?
Please note that I'm aware of solutions using DESCRIBE and select column_name from information_schema.columns where table_name='person';
but I need a more flexible solution that will fit these multicolumn queries.
Please also note that I am still using the original PHP MySQL extention (so no MySQLi, and no PDO).
If you wrap your query with the following SQL, it will always return the column names from your query, even if it is an empty query result:
select myQuery.*
from (select 1) as ignoreMe
left join (
select * from myTable where false -- insert your query here
) as myQuery on true
Note: When the results of the subquery are empty, a single row of null values will be returned. If there is data in the subquery it won't affect the output because it creates a cross-product with a single row...and value x 1 = value
Execute following command if the result of your previous query is empty
SHOW columns FROM your-table;
For more details check this.
I'm not sure if it will satisfy you but you can do this
SELECT *, COUNT(*) FROM table;
It will return null values (except last column which you can ignore) if the query is empty and you will be able to access all columns. It's not proper way of doing it and selecting names from INFORMATION_SCHEMA would be much better solution.
Please note that result is aggregated and you need to use GROUP BY to get more results if there are any.
You should ,
Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS
Where TABLE_SCHEMA='yourdb'
AND TABLE_NAME='yourtablename';

How to set name of the column using union

I making a SQL statement using union all as the following image:
But the name of the column is the first name used in SQL query. How make this SQL statement without this my error?
Thanks a lot!
Use as to make an alias name. You already do it with the count() column too but did not use as which is optional.
select 'abertos' as some_name, count(fk_id_status) ....
BTW you only need to do it for the first query in the union

Deleting all rows which contain a unique value

I have a database and my dataset is really messed up. The column of importance is the a "uniqueidentity" number where some records have "&&" or "%%" in contained at the end of the value. If it does, I would like to delete the entire row from the table. uniqueidentity = VARCHAR
Does anybody have any ideas on how to do this using a SQL Query?
Thanks in advance
you could use
DELETE FROM table WHERE RIGHT(uniqueidentity, 2) = "&&" OR RIGHT(uniqueidentity, 2) = "%%"
know thy sql commands young one and all troubles will begone
sql delete result from query
http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
SQL - how to select words with certain values at the end of word
Try like this:
DELETE FROM tableName WHERE
uniqueidentity REGEXP '%%$' OR
uniqueidentity REGEXP '&&$';
I have created supporting SQL FIDDLE with select query which you can change it to delete as above.

How to get minimum value from a set of values returned by SELECT query?

I have written a SELECT query, which will return a set of values, for ex.,
The following one is the actual table -
select data from tab1 where id <5; // This statement returns me the following table
I am trying to get the minimum value of the resultant table. I have tried the following query for that -
select MIN(select data from tab1 where id<5);
SQLite Browser says, there is an error in the select statement. My doubt is, whether we can give a select statement directly into an aggregate function like MIN()? If not, can you please suggest me a better way to do this task?
Thanks in advance.
Try this way:
select MIN(data)
from tab1 where id<5;