MySQL Tricky query [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 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

Related

Delete from select by many columns in MySql [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 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).

How to SELECT a common parameter and display in export using INNER JOIN? [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 last month.
Improve this question
The common parameter in all tables is ID (primary key).
Now I would like that the ID also appears in the export and at best then arrange all entries ascending with the ID.
But this seems to be not possible. PhpMyAdmin gives the error message
#1052 - Field 'ID' in field list is not unique.
Is there a way to export the ID as well?
My function has the following structure:
SELECT name, age, date, ID FROM table t1 INNER JOIN table t2 ON t1.ID = t2.ID INNER JOIN table t3 ON t2.ID = t3.ID ORDER BY t1.ID ASC;
Table 1
ID
name
Table 2
ID
age
Table 3
ID
date
Your SELECT ID part seems vague. Perhaps be explicit and state t1.ID.

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.

Mysql error when try to update a table from another 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 3 years ago.
Improve this question
I need to update a field in TableA from the same field on TableB
UPDATE TableA
SET TableA.categories = (
SELECT TableB.categories
FROM TableB
WHERE TableB.title = TableA.title
);
I get this error :
Error in query (1242): Subquery returns more than 1 row
If you are updating more than one row then subquery will not work, try using JOIN UPDATE
UPDATE TableA
JOIN TableB ON TableB.title = TableA.title AND TableB.uid = TableA.uid
SET TableA.categories = TableB.categories;

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