Mysql - join 3 tables with different number of rows into another [duplicate] - mysql

This question already has answers here:
How can I do a FULL OUTER JOIN in MySQL?
(15 answers)
Closed 5 years ago.
I have the following tables and I need to join them:
Table A
+----+----------+--------+
| ID | Period | Value |
+----+----------+--------+
| 1 |2009-02-01| 20.3 |
| 2 |2009-03-01| 22.5 |
| 3 |2009-04-01| 17.4 |
| 4 |2009-05-01| 16.5 |
| 5 |2009-06-01| 26.5 |
| 6 |2009-07-01| 35.4 |
+----+----------+--------+
Table B
+----+----------+--------+
| ID | Period | Value |
+----+----------+--------+
| 1 |2009-04-01| 57.1 |
| 2 |2009-05-01| 56.5 |
| 3 |2009-06-01| 59.8 |
| 4 |2009-07-01| 55.4 |
+----+----------+--------+
Table C
+----+----------+--------+
| ID | Period | Value |
+----+----------+--------+
| 1 |2009-03-01| 82.5 |
| 2 |2009-04-01| 87.4 |
| 3 |2009-05-01| 86.7 |
+----+----------+--------+
My output table is already created empty table and looks like this:
Table D
+----+----------+--------+--------+--------+
| ID | Period | ValueA | ValueB | ValueC |
+----+----------+--------+--------+--------+
As table A contains most of the records I want to use it as primary table. The desired result is as follow:
Table D
+----+----------+--------+--------+--------+
| ID | Period | ValueA | ValueB | ValueC |
+----+----------+--------+--------+--------+
| 1 |2009-02-01| 20.3 | NULL | NULL |
| 2 |2009-03-01| 22.5 | NULL | 82.5 |
| 3 |2009-04-01| 17.4 | 57.1 | 87.4 |
| 4 |2009-05-01| 16.5 | 56.5 | 86.7 |
| 5 |2009-06-01| 26.5 | 59.8 | NULL |
| 6 |2009-07-01| 35.4 | 55.4 | NULL |
+----+----------+--------+--------+--------+
I'm very new to MySQL. I looked to similar questions in the forum and tried to figure it out for myself but with no success.
Any help appreciated.
ANSWER
Ok. Few things. First the question isn't duplicate! The thing looks more like a speedy moderator than duplicate question. And No, I didn't find this answer anywhere on this website and for sure not in the suggested from the moderator answer.
Now the interesting part.
With the help of the creative moderator after spending few hours finally I got it work. As I said in the original question table D already exists. This is because the model is created and managed by third party application. In this case Django. This is an important point as otherwise the operation will be different. I also have more than one schema on the server.
I think In this case the best way for this query is by using alias as follow:
insert into my_schema.table_d(period, valueA, valueB, valueC)
select A.Period, A.Value, B.Value, C.Value
from my_schema.table_a A
left join my_schema.table_b B
on A.Period = B.Period
left join my_schema.table_c C
on A.Period = C.Period

You are looking for a left join:
insert into d( . . . )
select . . .
from a left join
b
on a.period = b.period left join
c
on a.period = c.period;
Fill in the columns where the . . . are.

Related

How to delete table rows that have not matches rows in second table

I have 2 tables in 1 database.
In the 2 tables there are several rows with the same contents.
table visitor
--------------------------
id | mytoken1 |
--------------------------
1 | token_abcd |
2 | token_efgh |
3 | token_ijkl |
4 | token_mnop |
--------------------------
table favorites
--------------------------
id | mytoken2 |
--------------------------
1 | token_abcd |
2 | token_efgh |
3 | token_ijkl |
4 | token_mnop |
5 | token_aaaa |
6 | token_bbbb |
7 | token_cccc |
8 | token_dddd |
--------------------------
How do I delete the mytoken2 column that is not in the mytoken1 column?
So in the example above I want to delete 4 rows of data, including:
token_aaaa
token_bbbb
token_cccc
token_dddd
I have tried to find a solution until I was dizzy but it has not been resolved, I hope someone will help me here ..
You can do using NOT IN
DELETE FROM favorites
WHERE token2 NOT IN (SELECT token1 FROM visitor)
You can use NOT EXISTS.
DELETE FROM favorites
WHERE NOT EXISTS (SELECT *
FROM visitor
WHERE visitor.mytoken1 = favorites.mytoken2);
JOIN also can be used here:
DELETE favorites.*
FROM favorites
LEFT JOIN visitor ON visitor.mytoken1 = favorites.mytoken2
WHERE visitor.id IS NULL;
Here you can test SQL query

MySQL Count within an IF

+-------------+--------------+----------+-------+
| ticketRefNo | nameOnTicket | boughtBy | event |
+-------------+--------------+----------+-------+
| 38 | J XXXXXXXXX | 2 | 13 |
| 39 | C YYYYYYY | 1 | 13 |
| 40 | M ZZZZZZZZZZ | 3 | 14 |
| 41 | C AAAAAAA | 3 | 15 |
| 42 | D BBBBBB | 3 | 16 |
| 43 | A CCCCC | 3 | 17 |
+-------------+--------------+----------+-------+
+-------------+------------------+--------------+---------------------+--------+
| ticketRefNo | cardNo | cardHolder | exp | issuer |
+-------------+------------------+--------------+---------------------+--------+
| 38 | 4444111133332222 | J McKenny | 2016-01-01 00:00:00 | BOS |
| 39 | 4434111133332222 | C Dempsey | 2016-04-01 00:00:00 | BOS |
| 40 | 4244111133332222 | M Gunn-Davis | 2018-02-01 00:00:00 | RBS |
+-------------+------------------+--------------+---------------------+--------+
+-------------+-------------+----------+
| ticketRefNo | boxOfficeID | paidWith |
+-------------+-------------+----------+
| 41 | 1 | card |
| 42 | 2 | cash |
| 43 | 3 | chequ |
+-------------+-------------+----------+
I have a database with the data shown above. It represents a ticket-buying system. I would like to be able to see a list of tickets bought with the name of the event and either the boxOfficeID or the issuer of the debit card.
I have tried running the following code, to no avail.
SELECT t.ticketRefNo AS 'Reference', t.event AS 'Event',
IF(COUNT(SELECT * FROM Online WHERE t.ticketRefNo=o.ticketRefNo;) >= 1,
o.issuer, InPerson.boxOfficeID) AS 'Card Issuer or Box Office'
FROM Ticket AS t, InPerson, Online AS o
WHERE t.ticketRefNo=o.ticketRefNo;
Cheers in advance!
Some notes: the semicolon character isn't valid syntax; if you have a need to delimit the subquery, wrap it in parens. Escape column aliases like you'd escape any other identifier: use backticks, not single quotes. Single quotes are used around string literals.
Assuming that issuer in the Online table is NOT NULL, and assuming that ticketRefNo is unique in both the Online and InPerson tables, you could do something like this:
SELECT t.ticketRefNo AS `Reference`
, t.event AS `Event`
, IF(o.ticketRefNo IS NOT NULL,o.issuer,i.boxOfficeId)
AS `Card Issuer or Box Office`
FROM Ticket t
LEFT
JOIN InPerson i
ON i.ticketRefNo = t.ticketRefNo
LEFT
JOIN Online o
ON o.ticketRefNo = t.ticketRefNo
Use outer join operations to find matching rows in the InPerson and Online tables, and use a conditional test to see if you got a matching row from the Online table. A NULL will be returned if there wasn't a matching row found.
It's not a good idea to have one column JOINing to two different tables with some values in each of the two tables.
But here goes anyway:
( SELECT ... FROM Ticket t JOIN InPerson x USING(ticketRefNo) ... )
UNION ALL
( SELECT ... FROM Ticket t JOIN Online x USING(ticketRefNo) ... )
ORDER BY ...
The ALL assumes that InPerson and Online never have any overlapping ticketRefNos.
The ORDER BY an the end is in case you want to sort things, although I see no need for it in your attempted SELECT.
The two SELECTs must have the same number of columns.

SQL join several tables based on latest entry in transaction table per join record

I have a transaction table with timestamps
a transaction has one event and one user.
All transactions have an event,
All events have at least one trasaction,
Each transaction has a user that must exist,
A User will not necessarily have a transaction.
The output will be a sort of the evt list
Output line count should equal db.evt record count.
The first column of each table is the Autoinc unique index.
In transaction, these are fks to the other tables.
The problem is that I need the transaction with the latest timestamp for the evt in the transaction table.
I am still relatively new to SQL (Using MySQL) and while I muddle through joins. I have no idea how to get the latest record by evID by timestamp.
I have looked at other questions on the topic but not found one that addresses mine. (Granted there are 14K on Joins alone, so I may have missed one)
Sample Table Data below:
Table structure is hopefully obvious by I will edit it in if requested.
Edit:
I've changed the names of tables and columns for clarity (and to avoid matching keywords)
I tried Stuart's answer below and got an error:
Answer:
SELECT
eventTable.EvtName AS EvtD,
transTable.TranAct AS LastTrans,
userTable.UserName AS UsrNm
FROM
transTables,
INNER JOIN (
SELECT evtID, MAX(TransID) TransID FROM transTable GROUP BY evtID
) last ON last.evtID = transTable.evtID AND last.TransID = transTable.TransID
INNER JOIN eventTable ON eventTable.evtID = transTable.evtID
INNER JOIN userTable ON userTable.usId = transTable.usId
Response:
#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near
'INNER JOIN (
SELECT evtID, MAX(TransID) TransID FROM transTable GROUP BY evt'
at line 7
Tables:
db.transTable
| TransID | EvtID | TranAct | timestamp | UserID
----------------------------------------------------------------
| 1 | 1 | add | 2014-05-08 08:10:00.000 | 3
| 2 | 2 | add | 2014-05-08 09:10:00.000 | 2
| 3 | 3 | add | 2014-05-08 10:10:00.000 | 3
| 4 | 2 | validate | 2014-05-08 11:10:00.000 | 5
| 5 | 3 | validate | 2014-05-08 12:10:00.000 | 3
| 6 | 2 | reverse | 2014-05-08 13:10:00.000 | 1
| 7 | 1 | edit | 2014-05-08 14:10:00.000 | 4
| 8 | 4 | add | 2014-05-08 15:10:00.000 | 3
| 9 | 5 | add | 2014-05-08 16:10:00.000 | 2
db.eventTable
| EvtID | EvtName
-----------------
| 1 | Evt1
| 2 | Evt2
| 3 | Evt3
| 4 | Evt4
| 5 | Evt5
db.userTable
| UserID | UserName
--------------------
| 1 | Usr1
| 2 | Usr2
| 3 | Usr3
| 4 | Usr4
| 5 | Usr5
Desired output:
eventTable.EvtName AS EvtD
transTable.TranAct AS LastTrans
userTable.UserName AS UsrNm
| EvtD | LastTrans | UsrNm
--------------------------
| Evt1 | edit | Usr4
| Evt2 | reverse | Usr1
| Evt3 | validate | Usr3
| Evt4 | add | Usr3
| Evt5 | add | Usr2
Much thanks for any assistance.
Something like this shuold work where a derived table is used to eliminate all transactions except the latest per evId.
SELECT
eventTable.EvtName AS EvtD,
transTable.TranAct AS LastTrans,
userTable.UserName AS UsrNm,
FROM
transTable
INNER JOIN (
SELECT evId, MAX(UID) uid FROM transTable GROUP BY evId
) last ON last.evId = transTable.evId AND last.uid = transTable.uid
INNER JOIN eventTable ON eventTable.evId = transTable.evId
INNER JOIN userTable ON userTable.usId = transTable.usId

MySQL: optimize query for scoring calculation

I have a data table that I use to do some calculations. The resulting data set after calculations looks like:
+------------+-----------+------+----------+
| id_process | id_region | type | result |
+------------+-----------+------+----------+
| 1 | 4 | 1 | 65.2174 |
| 1 | 5 | 1 | 78.7419 |
| 1 | 6 | 1 | 95.2308 |
| 1 | 4 | 1 | 25.0000 |
| 1 | 7 | 1 | 100.0000 |
+------------+-----------+------+----------+
By other hand I have other table that contains a set of ranges that are used to classify the calculations results. The range tables looks like:
+----------+--------------+---------+
| id_level | start | end | status |
+----------+--------------+---------+
| 1 | 0 | 75 | Danger |
| 2 | 76 | 90 | Alert |
| 3 | 91 | 100 | Good |
+----------+--------------+---------+
I need to do a query that add the corresponding 'status' column to each value when do calculations. Currently, I can do that adding the following field to calculation query:
select
...,
...,
[math formula] as result,
(select status
from ranges r
where result between r.start and r.end) status
from ...
where ...
It works ok. But when I have a lot of rows (more than 200K), calculation query become slow.
My question is: there is some way to find that 'status' value without do that subquery?
Some one have worked on something similar before?
Thanks
Yes, you are looking for a subquery and join:
select s.*, r.status
from (select s.*
from <your query here>
) s left outer join
ranges r
on s.result between r.start and r.end
Explicit joins often optimize better than nested select. In this case, though, the ranges table seems pretty small, so this may not be the performance issue.

MySQL Left join (I think) help

I am having trouble working out how to return the correct row in a table given that said table is relational. The two tables are shown below.
web_quote_models table
id | model | product_id | cpu_id | ram_id | hdd_id | os_id | opt_id
=========================================================================
1 | 000001 | 1 | 1 | 1 | 1 | 1 | 1
2 | 000002 | 1 | 2 | 2 | 2 | 2 | 2
3 | 000003 | 1 | 3 | 3 | 3 | 3 | 3
4 | Custom | 0 | 0 | 3 | 4 | 4 | 4
web_quote_component_cpu table
id | name
=========================================================================
1 | Intel® Core™ i3 2100 3.1GHz dual-core
2 | Intel® Core™ i5 2500 2.7GHz quad-core
3 | Intel® Core%trade; i7 2600 3.4GHz 8mb Cache dual-core
So what I need to achieve is a query that will look inside the web_quote_models table and match the model field with a $_SESSION['model'] then match the web_quote_models.cpu_id field with the web_quote_component.id.
This is what I have so far; I cant be too far off I think.
("
SELECT web_quote_component_cpu.name
FROM web_quote_component_cpu
LEFT JOIN web_quote_models
ON web_quote_component_cpu.id='web_quote_models.cpu_id'
AND web_quote_models.name='".$_SESSION['model']."'
");
A massive thank you in advance to anyone that helps.
Dan.
I don't think you're far off at all. I believe that all you need to do is stop quoting web_quote_models.cpu_id:
SELECT web_quote_component_cpu.name
FROM web_quote_component_cpu
LEFT JOIN web_quote_models
-- note lack of quotes in the following line:
ON web_quote_component_cpu.id=web_quote_models.cpu_id
AND web_quote_models.name='".$_SESSION['model']."'
Edit
Based on the comment, I would personally re-write the query:
SELECT web_quote_component_cpu.name
FROM web_quote_component_cpu WHERE ID IN
( SELECT ID FROM WEB_QUOTE_MODELS WHERE
web_quote_models.name='".$_SESSION['model']."' );
Though I would suspect that swapping that last AND with a WHERE would be sufficient:
SELECT web_quote_component_cpu.name
FROM web_quote_component_cpu
LEFT JOIN web_quote_models
-- note lack of quotes in the following line:
ON web_quote_component_cpu.id=web_quote_models.cpu_id
WHERE web_quote_models.name='".$_SESSION['model']."'