i have two tables main_table and new_data and i would like to update main_table by data from new_data table as you can see there are several empty places in time column in main_table. It should be fill in by data from new_data table. The 3rd table is the result. What is the best solution for this?
main_table
---------------------
id | name | time
---------------------
1 | tom | 60
2 | daniel | 30
3 | monica | 42
4 | gabriela |
5 | rachel |
6 | michael | 15
7 | adriana |
---------------------
new_data
--------------------
id | name | time
--------------------
1 | gabriela | 22
2 | rachel | 15
3 | adriana | 17
--------------------
main_table - updated by new_data - it should be result
---------------------
id | name | time
---------------------
1 | tom | 60
2 | daniel | 30
3 | monica | 42
4 | gabriela | 22
5 | rachel | 15
6 | michael | 15
7 | adriana | 17
---------------------
UPDATE new_data t1, JOIN main_table t2
SET t2.Time=t1.Time
WHERE t2.name=t1.name
Related
How can I fetch the full row that contain the max(column).
Example:
I have a table like this
+----+---------------+------+---------------------+
| id | title | c | update |
+----+---------------+------+---------------------+
| 1 | John S. | 15 | 2017-09-15 09:04:13 |
| 1 | John S. | 21 | 2017-09-15 09:04:29 |
| 1 | John S. | 22 | 2017-09-16 01:55:26 |
| 43 | Cristover Co. | 15 | 2017-09-17 14:11:43 |
| 43 | PeterSberg R. | 16 | 2017-09-17 15:12:30 |
| 43 | Cristover Co. | 19 | 2017-09-17 21:45:10 |
+----+---------------+------+---------------------+
Now I want to select the row that contains the max(update). Expected result:
+----+---------------+------+---------------------+
| id | title | c | update |
+----+---------------+------+---------------------+
| 43 | Cristover Co. | 19 | 2017-09-17 21:45:10 |
+----+---------------+------+---------------------+
First, you shouldn't name your column update, choose another name, let's say "date_of_update"
If you want to select only the row with the most recent update, you can simply use something like this :
SELECT * FROM table_name ORDER BY date_of_update DESC LIMIT 1.
This will give you only one row with the max date!
try this one
SELECT * FROM table_name
WHERE table_name.update =
(SELECT max(table_name.update) from table_name)
I have a little bit issue about MYSQL query
So I have table named "Diagnose" and "Diagnose Master"
in "Diagnose" there is an related field & record
idd | idmed | idf1 | idf2 | idf3
1 | 20 | 5 | 8 | 9
2 | 21 | 3 | - | 11
3 | 22 | 7 | 1 | -
4 | 23 | 1 | - | -
5 | 24 | 6 | 2 | 8
...
in "Diagnose Master"
iddm | code | name
1 | A.1 | ABC
2 | A.2 | ABCD
3 | B.3 | ABCDE
4 | B.4 | ABCDEF
5 | C.5 | ABCDEFG
...
I need to select diagnose table with idf1, idf2, idf3 field replace by field name from diagnose master so the output will be
idd | idmed | idf1 | idf2 | idf3
1 | 20 | ABCDEFG | ABCDEFGH | ABCDEFGHIJ
2 | 21 | ABCDE | - | ABC
...
How to query that?
Thank You
You need to join DiagnoseMaster three times to the same table on three different columns, using three different aliases. Try something like this:
SELECT d.idd, d.idmed, dm1.name idf1, dm2.name idf2, dm3.name idf3
FROM Diagnose d
JOIN DiagnoseMaster dm1 on dm1.idd = d.idf1
JOIN DiagnoseMaster dm2 on dm2.idd = d.idf2
JOIN DiagnoseMaster dm3 on dm3.idd = d.idf3
I have a table that looks like this if I 'select *'
+----+--------+------+------------+
| id | name | task | day |
+----+--------+------+------------+
| 1 | Rodney | 2 | 2016-05-05 |
| 2 | Rodney | 2 | 2016-05-08 |
| 3 | Rodney | 8 | 2016-05-08 |
| 4 | Scott | 2 | 2016-05-05 |
| 5 | Scott | 8 | 2016-05-05 |
| 6 | Frank | 2 | 2016-05-05 |
| 7 | Frank | 2 | 2016-05-08 |
+----+--------+------+------------+
What I'm trying to achive is a query that will get the last entered 'task' for each person. So, in this case I would want back:
2 | Rodney | 2 | 2016-05-08
3 | Rodney | 8 | 2016-05-08
4 | Scott | 2 | 2016-05-05
5 | Scott | 8 | 2016-05-05
7 | Frank | 2 | 2016-05-08
I'm pretty sure I need to use distinct against name & task and max for the most recent entry. Just not sure how to structure the two of them together to get the result.
select distinct name, task from test;
Gets me close...
+--------+------+
| name | task |
+--------+------+
| Rodney | 2 |
| Rodney | 8 |
| Scott | 2 |
| Scott | 8 |
| Frank | 2 |
+--------+------+
But no date...My SQL is limited. Any help would be appreciated.
Aggregate your rows so as to get the latest day per name. Then access the table again to get the records matching thse days:
select *
from test
where (name, day) in
(
select name, max(day)
from test
group by name
);
Another way is to select the records for which not exists a later record for the same name:
select *
from test
where not exists
(
select *
from test later
where later.name = test.name
and later.day > test.day
);
So, I have 3 tables :
guest :
id_guest | name
1 | John
2 | Nick
3 | James
4 | Paul
5 | Chris
6 | Karen
7 | Peter
room :
id_room | status
1 | Clean
2 | Dirty
3 | Dirty
4 | Clean
5 | Clean
6 | Clean
reservation :
id_guest | id_room | date
1 | 1 | 2015-04-15
1 | 1 | 2015-04-16
1 | 1 | 2015-04-17
2 | 3 | 2015-04-15
3 | 4 | 2015-04-15
3 | 4 | 2015-04-16
4 | 2 | 2015-04-16
5 | 2 | 2015-04-17
6 | 2 | 2015-04-18
And this is what the expected output should be :
id_room | status | d04-15 | d04-16 | d04-17 | d04-18
1 | Clean | John | John | John |
2 | Dirty | | Paul | Chris | Karen
3 | Dirty | Nick | | |
4 | Clean | James | James | |
5 | Clean | | | |
6 | Clean | | | |
I have been able to show it until the third field (d04-15) though with the date as values, using :
SELECT room.id_room,
room.status,
reservation.date AS d04-15
FROM room
LEFT JOIN reservation
ON room.id_room = reservation.id_room AND reservation.date = '2015-04-15'
GROUP BY room.id_room
But I'm not sure as to how to display the name there and
appending new fields (d04-16, d04-17, and d04-18) from another JOIN statement.
Any help would be appreciated. Thanks.
The columns returned in a query can't be altered at run time; they have to be statically declared in the SQL SELECT statement.
Here's an example of a statement that can achieve the specified result:
SELECT m.id_room
, m.status
, MAX(IF(r.date='2015-04-15',g.name,NULL)) AS `d04-15`
, MAX(IF(r.date='2015-04-16',g.name,NULL)) AS `d04-16`
, MAX(IF(r.date='2015-04-17',g.name,NULL)) AS `d04-17`
, MAX(IF(r.date='2015-04-18',g.name,NULL)) AS `d04-18`
FROM room m
LEFT
JOIN reservation r
ON r.id_room = m.id_room
AND r.date IN ('2015-04-15','2015-04-16','2015-04-17','2015-04-18')
LEFT
JOIN guest g
ON g.id_guest = r.id_guest
GROUP BY m.id_room
I had a technical interview last week, and my interviewer asked me what happens if I run the following query:
SELECT * FROM tbl1, tbl2
I think I answered it correctly, but it wasn't an in-depth answer.
I said that I would select all the columns in both tables. For example if tbl1 has 3 columns, and tbl2 has 4 columns. The result set would have 7 columns.
Then he asked me why 7? and I said because I was selecting everything from each table.
That was a bad answer, but I couldn't think of anything else.
To cut to the chase, after the interviewed I executed the latter statement using two tables.
Table A, had 3 animal: dog, cat and elephant.
Table B had 2 names: Mat and Beth
This is the result set that I got after the statement being executed:
*********************************************
| id_tbl1 | name_tbl1 | id_tbl2 | name_tbl2 |
*********************************************
| 1 | dog | 1 | Mat |
| 2 | cat | 1 | Mat |
| 3 | elephant | 1 | Mat |
| 1 | dog | 2 | Beth |
| 2 | cat | 2 | Beth |
| 3 | elephant | 2 | Beth |
*********************************************
So my question is, why does the statement behaves like that?
In other words:
Why does the Table B's records repeat themselves until I reach the end of table A, and then it starts all over again?
How would you have answered the question in a way that it would've "WOW'd" the interviewer?
If this question does not belong to SO, feel free to delete it or close it!
If you do a select like this, all rows in one resultset are joined to all rows in the other resultset (Cartesian Product).
So you get a list of all rows of the first table with the first row of the second table, Then all entries for the second row and so on. The order may be an implementation detail. Not sure if it is defined that the first order is by the first table, it might be different across implementations.
If you join three tables (or more), then the same happens with all rows of all tables. This, of course, is not only for tables, but for any result set from joins.
The result will be a cartisian product
take a look at this example
SQL Example
You can see there are two tables one has 5 records and the other has 4 and the result is 20 records. Means 5 * 4 = 20 instead of 5 + 4 = 9 as you are assuming.
Table1
| IDX | VAL |
---------------
| 1 | 1val1 |
| 1 | 1val2 |
| 2 | 2val1 |
| 2 | 2val2 |
| 2 | 2val3 |
Table2
| ID | POINTS |
---------------
| 1 | 2 |
| 2 | 10 |
| 3 | 21 |
| 4 | 29 |
Result of below query
SELECT * FROM Table1 , Table2
| IDX | VAL | ID | POINTS |
-----------------------------
| 1 | 1val1 | 1 | 2 |
| 1 | 1val1 | 2 | 10 |
| 1 | 1val1 | 3 | 21 |
| 1 | 1val1 | 4 | 29 |
| 1 | 1val2 | 1 | 2 |
| 1 | 1val2 | 2 | 10 |
| 1 | 1val2 | 3 | 21 |
| 1 | 1val2 | 4 | 29 |
| 2 | 2val1 | 1 | 2 |
| 2 | 2val1 | 2 | 10 |
| 2 | 2val1 | 3 | 21 |
| 2 | 2val1 | 4 | 29 |
| 2 | 2val2 | 1 | 2 |
| 2 | 2val2 | 2 | 10 |
| 2 | 2val2 | 3 | 21 |
| 2 | 2val2 | 4 | 29 |
| 2 | 2val3 | 1 | 2 |
| 2 | 2val3 | 2 | 10 |
| 2 | 2val3 | 3 | 21 |
| 2 | 2val3 | 4 | 29 |
I think you are confusing yourself by running an example with two tables that have identical fields. You are referring to a Union, which will combine the values of 1 table with another, and using your example this would give you 3 + 4 = 7 results.
The comma separated FROM statement is doing JOIN, which will go through all values in Table X and pair them with all the values of Table Y. This would result in Size of X * Size of Y results, and using your example this would be 3 * 4 = 12.