Delete from select by many columns in MySql [closed] - mysql

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 last month.
Improve this question
Probably it's a simple question, many people asked similar questions before, but it's very hard to find an answer for my specific case.
I have a complex query like that:
SELECT val1, val2, val3
FROM table1
LEFT JOIN ...
WHERE ...
HAVING ...
Now I want to delete some rows from another table (let's call it table2) after joining with the result of the previous statement. Like that:
DELETE FROM table2 WHERE field1=val1 AND field2=val2 AND field3=val3
Is it possible to do that with a single query?

You can use IN operator as the following:
DELETE FROM table2
WHERE (field1, field2, field3) IN
(
SELECT val1, val2, val3
FROM table1
LEFT JOIN ...
WHERE ...
HAVING ...
)
See more info about using IN operator from the MySql reference manual.

According to MySQL 8.0 Reference Manual, section "Multi-Table Deletes" this syntax is one of the two given alternatives:
DELETE Table2
FROM Table2
INNER JOIN Table1
WHERE Table1.val1=Table2.val1
AND Table1.val2=Table2.val2
AND Table1.val3=Table2.val3;
Also note, that your are deleting rows and not table(s).

Related

Request in MySQL in two tables [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
MySQL. I'm working with database and i have 2 tables. 1st table is mane, it has id and NAME. 2nd connected with 1st via FOREIGN KEY and has COST. How to get such request that has NAME of 1st and sum of COST of 2nd tables?
Just join, group by and sum(). Assuming that the foreign key column in table2 is t1_id:
select t1.name, sum(t2.cost) total_cost
from table1 t1
inner join table2 t2 on t2.t1_id = t1.id
group by t1.id, t1.name
If you want to allow names that have no match in table2, then use left join instead, and maybe coalesce(sum(t2.cost), 0) as well.

How can I apply a rule to update records in a field based on a condition and by grouping the data [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
I am trying to do this task in sql. but I am not able to derive a logic that helps getting me this output.
It looks like you want to bring the name of the player whose code is "x" for each id. If so, one option uses window functions:
select id, playercode, playername,
max(case when playercode = 'x' then playername end) over(partition by id) requiredoutput
from mytable
If your database does not support window functions, one option uses a correlated subquery. Assuming no duplicates on (id, playercode):
select id, playercode, playername,
(
select t1.playername
from mytable t1
where t1.id = t.id and t1.playercode = 'x'
) requiredoutput
from mytable t
If you're looking for an update statement maybe something like this would work
with upd_cte as (
select distinct id, player_name
from tTable
where Playercode='x')
update t
set requiredoutput=u.player_name
from tTable t
join upd_cte u on t.id=u.id;

Accessing data from more than one table - SQL [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 6 years ago.
Improve this question
Do tables have to be joined to extract data from them?
I believe you mean in one query. There are two ways I can think of to extract data for more than one table in a single query without "joining" them:
Fist is with union
SELECT A, B, C FROM Table1
UNION
SELECT X, Y, Z FROM Table2
You can also do a "cross join" which does not look like a join (and is probably what you are thinking of)
SELECT Table1.A, Table1.B, Table1.C, Table2.X, Table2,Y, Table2,Z
FROM Table1, Table2
As you can see from the syntax there is not relationship from one table to the other. This means that every row in table1 will be combined with every row in table2!
In my experience this is the most common mistake that programmers new to SQL make. They do a cross join when they mean a join and then they GROUP BY or DISTINCT to get the results they want. This is hugely inefficient!
Cross join can be good however, especially when you one of the table has just one row -- then you are adding those values to every row in other table. You are basically selecting single set of values for columns for every row.
Like if you want every row to have the maximum date (this is often done in reports)
SELECT *
FROM Table1, (SELECT MAX(updatedate) as Max_Update FROM Table1) AS MaxDate
Not necessarily, there are other options like Union:
SELECT customerNumber id, contactLastname name
FROM customers
UNION
SELECT employeeNumber id,firstname name
FROM employees
the above example is take from this.
There are other times you can run multiple queries, etc to get data from multiple sources with out a join. It all depends on what you want to do. However, join is a very common--and probably most usual--approach.

MySQL Tricky query [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 8 years ago.
Improve this question
I have two tables
TABLE1 : ID(PRIMARTY-INT) COLUMN1(CHAR),LAST_MODIFIED TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
TABLE2 : ID(INT) COLUMN1(CHAR)
TABLE2 contains about 1000 records.
If I use
REPLACE INTO TABLE1(ID,COLUM1) SELECT ID,COLUMN FROM TABLE2
then LAST_MODIFIED will be updated even if previous record is same.
If I use INSERT, it will give an error on duplicate record.
If I use INSERT IGNORE, it won't update the record.
What I want: update/add new values and also update LAST_MODIFIED value if new values are not same as old values.
You can do it like this:
REPLACE INTO TABLE1(ID,COLUM1)
SELECT TABLE2.ID,TABLE2.COLUMN
FROM TABLE2
INNER JOIN TABLE1 on (TABLE1.ID = TABLE2.ID) AND (TABLE1.COLUMN <> TABLE2.COLUMN)
or you could try this:
UPDATE table1 SET
table1.field1 = table2.field1
....
FROM table1
INNER JOIN table2 on table1.id = table2.id

Select SQL Query for two table? [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 8 years ago.
Improve this question
I have two tables
Table A : Content Question , its options and correct answer
Table B : This table is for time allow to answer the question in given time.
this table has Question_Id field which either have question id or zero. zero means if for Table A Question Id is not found in Table B then default Time will be Table B's Question_Id=0 > 5 Min
Now I want the data like Result table from query. By using Select Query with Join I am getting question details, which are matched with question Id (1,2,4) means for Question 3,5,6 row not getting that showing in result table.
Please suggest what sql query should write so that I can get result like Result Table's content.
I change your tables to small and simple tables and you can see the result in:
SQL Fiddle
or try this query:
SELECT t1.questionid,
t1.question,
t1.options,
t1.answer,
COALESCE(t2.timingstatement, '5 Min') TimingStatement
FROM tablea t1
LEFT OUTER JOIN tableb t2
ON t1.questionid = t2.questionid;
Try this
SELECT Q.QuestionID,Q.Question,Q.Options,QAnswer,
CASE WHEN Q.QuestionID NOT IN (SELECT QuestionID FROM Table2) THEN '5 Min'
ELSE T.TimingStatement
END [TimingStatement]
FROM Table1 Q
JOIN Table2 T ON Q.QuestionID = T.QuestionID