MySQL replace result value with another value - mysql

I have MySQL and the query is:
select name,re_type from myTable
I want to replace all values of type:
1 = student
2 = teacher
So the result should be:
name re_type
---------------
Ron Student
Mac Teacher
Not like:
name re_type
---------------
Ron 1
Mac 2
Is it possible to make a query like that so I get the desired result in MySQL ?

You can use a CASE statement
SELECT name, CASE WHEN re_type = 1 THEN 'Student' WHEN re_type = 2 THEN 'Teacher' ELSE 'Donno' END AS re_type_text
FROM myTable

You can use a joint table that will store labels for your re_type like
re_type_id re_type_label
1 student
2 teacher
And alter your query by :
select t.name,l.re_type_label
from myTable t
inner join labelsTable l
on l.re_type_id = t.re_type

I think he wants to keep IDs on re_type field, and just decoding them when extracting.
You can use CASE WHEN or ELT(), MySql equivalent to Oracle's Decode(), like explained here
, but the Best Practice is to create an external table containing re_type and re_type_description fields, so if tomorrow you'll have new values, you don't have to change all your queries.

Related

MySQL if result null change query

what if I have two strings: "123" and "abc". I want to select username if there's username "123" then choose it, if not found (null) then select username where "abc"
I have a table called USERS, this table responsibility with workflow engine account. I want to show columns in USERS:
username
email
usr_firstname
usr_lastname
I am using concat to merge column 3 and 4 with space between it. In the office, there are 2 types of employee:
origin/internal employee
outsource/partner employee
Origin employee login into every system using LDAP (FirstName.LastName), but outsource or partner employee login individually just for our workflow engine using employee identity number.
In this case, if I use something like:
Where username = 'employeenumber' or username = 'LDAPacc' the result is both account (used and unused for outsource) they appear. I want to show just 1 rows and 1 query but it's work with internal or even outsource (they will got data correctly for outsource).
You can use like this query;
SELECT *
FROM TABLE
WHERE username IN ('123', 'abc')
AND (username='123' OR NOT EXISTS(SELECT * FROM TABLE WHERE username='abc'))
You could use COALESCE.
COALESCE selects the first non null value out of the ones supplied.
So you could use....
SELECT COALESCE(String_123, string_ABC);
If string_123 has a value it will select that, otherwise it will select string_ABC unless of course they are both null.
So to be safe include a default value.......
SELECT COALESCE(String_123, string_ABC, string_Default);
I've found when I tested my logic to mysql tryit editor by w3schools and It's worked properly what I need. Here's my query:
SELECT * FROM Customers WHERE CustomerID = 'zz' OR (NOT EXISTS (SELECT * FROM Customers WHERE CustomerID = 'zz') AND CustomerID = '3')
let's say CustomerID is equivalent to my username column, then I tried to swap 'zz' and '3' value and it's still works. I hope there's more simple query than this

UPDATE MySQL query error "Subquery returns more than 1 row"

I'm trying to achieve this query but I got an error:
UPDATE ps_product_lang
SET name=(select name from ps_product_lang_backup where id_lang=2)
WHERE id_lang = 3
But I got the Subquery returns more than 1 row
Probably I must use JOIN but I'm really new to MySQL and cannot do myself.
What i'm trying to do is simple: i have in my database 3 languages, wish to copy data from one language (english id_lang 2) to paste in another (russian, id_lang 3)
I assume the table also has a product_id column that is unique for a product.
You need to tell the database to pick the english name for the same product.
UPDATE ps_product_lang
SET name=(select name from ps_product_lang_backup
where id_lang=2
and ps_product_lang.product_id = ps_product_lang_backup.product_id)
WHERE id_lang = 3
I'll try my best to answer this
after looking to your query it is clear that (select name from ps_product_lang_backup where id_lang=2) has more then one result that's why it's causing you the error. Now, to fix this you can do two things.
Either you can delete one record with the same id_lang(easy one).
OR, you can change your subquery like this:
select name from ps_product_lang_backup where id_lang=2 LIMIT 1
which will change your query like this:
UPDATE ps_product_lang SET name=(select name from ps_product_lang_backup where id_lang=2 LIMIT 1) WHERE id_lang = 3
Hope this help
Here it means that your SELECT Query Return more than two results Try this
SET name=(select name from ps_product_lang_backup where id_lang=2 LIMIT 1)

I need a desired table format in sql

TeacherApprover Table - Table format in Database
TeacherID ApproverID ApproverLevel
15122 4 1
15122 2 2
15122 3 3
i need the result as in select Statement- it should display as below
TeacherID ApproverID ApproverID2` ApproverID3
15122 4 2 3
i tried directly binding in front end but i have some issues after binding like this, So i want to try getting the result in Dataset directly from backend.
please guide me
I think you need approver id by teacher Id.
So I think you can get data using query,
SELECT ApproverID from table_name where teacherID = 15122;
Then handle the result according to your requirement.
Create new table having column as you needed.
insert into that new table .
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

In SQL if where condition does not yield result fetch other rows

I have Query like this
Select * from customers where id = 123 and name like '%tester%';
If id : 123 and name : "tester" doesn’t exist in table i should fetch other rows with name "tester" discarding condition "id". if it exists fetch row for that id and name.
Guys i know this is can be handled in program, i want this to be done in my Query, can you please STOP DOWN VOTING and give me the solution if you know!!!
You can try something like this:
SELECT *
FROM CUSTOMERS
WHERE ( ID = 123
AND NAME LIKE '%tester%' )
OR ( NAME LIKE '%tester%'
AND NOT EXISTS (SELECT *
FROM CUSTOMERS
WHERE ID = 123
AND NAME LIKE '%tester%') )
You can find a working example on SQL Fiddle.
SELECT * FROM customers WHERE (id = 123 AND name like '%tester%') OR (name LIKE '%tester%') LIMIT 1;
It should work then
I gotcha here, check it
Select * from customers where id = 3 or name like '%tester%'
order by id=3 desc limit 1;
See you will get all rows that have either id= 3 or name is like tester. From there you will order then by the boolean value (1 if true) by if they == 3. Limiting this to 1 result will get you only the best response.
Likewise if you want to get all results you could remove the limit, assume the top one is the best result. If the first results id is not = 3 then you could say that all the results are just best matches.

Inner join A on B if B not empty, else A

Two tables:
prefix ( id, value )
---------------------
1 'hello'
2 'good afternoon'
3 'good night'
suffix ( id, value )
---------------------
1 'world'
3 'world'
I'd like to get
all from table prefix which can be joined on table suffix via id
result should look like:
prefix.id prefix.value
--------------------------
1 'hello'
3 'good night'
well - quite easy so far...
but if table suffix is empty I'd like everything from table prefix
without subselects/ctes or if.... and in one query fulfilling both conditions!
Is there any trick to get this done by some magic having-clause or tricky something else?
Just for testcases: SQL-fiddle
Well, there is a way, but I agree with others that your requirements make no (practical) sense.
Anyway, here you go:
Join the suffix table twice (each time with a left join). One join is on the id column, the other on an always true condition.
Group the results on the prefix columns you want in the output and at least one non-nullable column of the first instance of suffix.
In the HAVING clause, put a condition that the first suffix column is not null or the number of values of a non-nullable column in the second suffix instance is 0. (Obviously, every group will have the same number of rows, i.e. the count will be the same for every prefix row.)
This is the query:
SELECT prefix.id, prefix.value
FROM prefix
LEFT JOIN suffix ON prefix.id = suffix.id
LEFT JOIN suffix AS test ON 1=1
GROUP BY prefix.id, prefix.value, suffix.id
HAVING suffix.id IS NOT NULL OR COUNT(test.id) = 0;
And there's also a demo at SQL Fiddle.
You need an OR and NOT EXISTS:
SELECT
prefix.id, prefix.value
FROM
prefix
WHERE
EXISTS(SELECT 1 from suffix WHERE prefix.id=suffix.id)
OR NOT EXISTS(SELECT 1 FROM suffix)
Demo
I guess the answer is: no, you can't!
Or if you can: No, you shouldn't.