SQL: UPDATE statement [closed] - mysql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Update the emp table, move the employee with the highest commission to department number 40, make him/her a manager and give him/her a pay raise of £1000. Hint : Use a subquery and >= all as with the ALL and ANY slide on the SQL Commands Handout.
All I seem to get:
Any ideas?

Your query is:
update emp
set . . .
where comm = max(comm);
The error is correct for several reasons. The simplest is that aggregation functions do not belong in a where clause; nor do they belong in an update.
If you want to update one row, you would do:
update emp
set . . .
order by comm desc
limit 1;
If you want to update all rows where comm is maximum (and there could be multiple such rows), then use join:
update emp e join
(select max(comm) as maxcomm from emp) ee
on e.comm = ee.maxcomm
set . . .;
I should note that neither of these are standard SQL. The standard SQL method (which MySQL does not support) is:
update emp
set . . .
where comm = (select max(e2.comm) from emp e2);

Related

Why this RIGHT join returns invoices with state other then pending? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm building library which generates SQL queries and found case which i don't understand why it is happening so. I agree that in general you should filter out by "where" and not "on" yet ON allows to filter out for LEFT join. Though for RIGHT join i get results that do not match ON request. I wonder why RIGHT join works this way.
Users (table name _user):
Invoices (table name _invoice):
Query:
SELECT
_user.id AS userId,
_user.name AS userName,
_user.state AS userState,
_invoice.id AS invoiceId,
_invoice.userId AS invoiceUserId,
_invoice. `state` AS invoiceState
FROM
_user
RIGHT JOIN _invoice on _invoice.state = 'pending'
Response:
Question:
Would be best to get step-by-step execution for this query-result to understand how exactly it happen.
A right join keeps all rows from the second table regardless of whether the on clause evaluates to "true", "false", or NULL.
Presumably, though, you want a valid join condition. My guess is you want a list of all users and any pending invoices. If so, the correct logic would be:
SELECT . . .
FROM _user u LEFT JOIN
_invoice i
ON i.userid = u.id AND i.state = 'pending';
At the very least, this produces a result that seems usable.

Why does my simple select SQL query fail? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
as part of an inner query I need to select the latest department number (dept_no) where each employee (emp_no) has worked. Note, some employees have worked in multiple departments. The name of this table is dept_emp.
It seems very simple (and probably is), but I haven't been able to figure it out. This is one of the queries I have tried
SELECT dept_no, emp_no, from_date
FROM dept_emp
GROUP BY emp_no
HAVING MAX(from_date);
This doesn't return the right results. for employee "10010" it returns dept_no "d004" and from_date "1996-11-24" while I expected "d006" and "2000-06-26"
Can anyone suggest a working query to me?
Your query is malformed. If MySQL accepts it you are probably running MySQL 5.x. MySQL 8.x does not accept those malformed queries by default anymore.
In order to get the rows you want you can use a subquery. For example:
select
d.dept_no,
d.emp_no,
d.from_date
from dept_emp d
join (
select emp_no, max(from_date) as max_from_date
from dept_emp
group by emp_no
) m on m.emp_no = d.emp_no
and m.max_from_date = d.from_date

MySQL - Select specified record with some conditions multiple table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I have a little trouble with a MySQL query, I have two tables tb_user and tb_trx and the last one is the result which I'm trying to do
What I want to do is to show id_user from tb_user which have level = 0 and if id_user in tb_trx have status = 0 more than equals 2, they will not appear in result.
Have a go with this (N.B. I haven't tested it):
select u.id_user from tb_user u
where u.level = 0
and (select count(*) from tb_trx t
where t.id_user = u.id_user
and t.status = 0) <= 2
Incidentally, I don't think your data set would properly test the rule you have given (which I may have misunderstood, your statement of it is pretty unclear).

What is wrong in my SQL Statement for select a table with foreignkeys? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
hi i have problems with my sql statements.
Here is my structure:
[![enter image description here][1]][1]
first sql: Here I have problems with Foreign Keys :(
Select Wohnung.Wohnungsnr, Objekt.Objektbezeichnung,
Hausmeister.Hausmeistername
from Objekt
group by Wohnung.Wohnungsnr
second sql: I need the average over all all Houses
select Objektnr, AVG(Miete)
from Mieter
group by Wohnungsnr
third sql:
select Wohnung.Wohnungsnr, Objekt.Objektbezeichnung,
Hausmeister.Hausmeistername, Mieter.Mietername
from Wohnung
group by Wohnungsnr
You need to join the tables
Select Wohnung.Wohnungsnr, Objekt.Objektbezeichnung,
Hausmeister.Hausmeistername
from Objekt
join Wohnung on Objekt.Objektnr=Wohnung.Objektnr
join Hausmeister on Objekt.Hnr=Hausmeister.Hnr
group by Wohnung.Wohnungsnr
The join could also be written as
join Wohnung using (Objektnr)
join Hausmeister using (Hnr)
NOTE - the parentheses are required

MySQL check if value exist more than once [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have MySQL database and a have a table named CALLER. In table caller I need to check if in column USERNAME there are values which exist more than once and if exist to list all these values.
Thank you
Ususlly you can do this using GROUP BY and HAVING COUNT(*)>1
SELECT USERNAME FROM CALLER
GROUP BY USERNAME
HAVING COUNT(*)>1
Update: To get all duplicate rows:
SELECT * FROM CALLER WHERE USERNAME IN
(
SELECT USERNAME FROM CALLER
GROUP BY USERNAME
HAVING COUNT(*)>1
)
Before getting to the answer....
It would have been a help if you'd provided the full table structure (as a CREATE TABLE statement)
If you need to apply this exercise it implies your database design is wrong - and after you've identified the duplicates and resolved them then you should add a unique index on the relevant column.
Assuming that you've got an auto-increment field, or some other value (such as created date) which differentiates between rows with the same USERNAME....
SELECT a.id, username, b.id
FROM caller a
INNER JOIN caller b
ON a.username=b.username
AND b.id>a.id
Note that this will report some rows more than once if the username exists for more than 2 rows. Alternately:
SELECT username, COUNT(DISTINCT id), MIN(id), MAX(id)
FROM caller
GROUP BY username
HAVING count(*)>1
But this won't explicitly identify all the ids where there are more than 2 rows with a specific username.