MySQL update with data from two tables - mysql

I have a MySQL database with the two tables that I need modified.
The first table holds notes
id type note
1 1 24 months warranty
2 1 12 months warranty
3 2 Garage in Denver
4 3 Pre sales maintenance done
....
And then a vehicle table that holds many vehicle tables and a field that hold notes with their text instead of a pointer
id licence_plate ... sales_notes ...
1 XH34DN ... <warranty>24 months warranty</warranty><garage>Garage in Denver</garage><maintenance>Pre sales maintenance done</maintenance> ...
2 K4B3C6 ... <warranty>12 months warranty</warranty><garage>Garage in Sacramento</garage><maintenance>Pre sales maintenance not done</maintenance> ...
As you can imagine this is higly inneficient and I want to modify to pointers that hold the id of the note.
id licence_plate ... warranty_note garage_note maintenace_note ...
1 XH34DN ... 1 3 4 ...
2 K4B3C6 ... 2 7 12 ...
I can do it manual updates but I would like to build one that makes it automatically by type.
So for notes.type=1 if the notes.note text is found in vehicle.sales_notes it updates the vehicle.warranty_note.
Any idea how to build something like that?
I have something like this in mind, but id doesn't work. No results are updated
UPDATE tx_vehicle v, tx_note n
SET v.garage_note = n.uid
WHERE v.sales_notes LIKE ('%'+n.note+'%')

MySQL has special XML parsing functions.
insert into your_new_notes_table (id, licence_plate, warranty_note, garage_note, maintenace_note)
select
sn.id,
sn.license_plate,
(select nt.id from notes as nt where nt.type = 1 and nt.note = ExtractValue(sn.sales_note, "/warranty")) as warranty_note,
(select nt.id from notes as nt where nt.type = 1 and nt.note = ExtractValue(sn.sales_note, "/garage")) as garage_note,
(select nt.id from notes as nt where nt.type = 1 and nt.note = ExtractValue(sn.sales_note, "/maintenance")) as maintenance_note
from note_types as nt
Although, it will fail if exact note from sales is not found in note_types table. You can adjust note text comparison, replacing it with like operator, regex checking or another functions; you can replace abbreviations with full words or invent your own MySQL function. Moreover, you can invent your own MySQL function that would encompass internal selects and return null if note is not found in note_types table.
If you want to update existing table, you need to perform update query with join to same select as I provided.

Related

Reorganizing mysql aggregate row into single piece rows

Consider the following mysql table:
ID WeightS AmountS WeightM AmountM WeightL AmountL Someothercolumnshere
1 6 3 10 2 18 2 ...
I need to reorganize this data into a pivot-friendly table, where each piece in the amount columns should be one result row. E.g. from the first two columns, WeightS and AmountS, the SELECT should produce 3 result rows, each having a weight of 2 kgs (=6 kgs total). So the full result table should be like this:
Weight Someothercolumnshere
2 ...
2 ...
2 ...
5 ...
5 ...
9 ...
9 ...
I don't even know if there's a SQL syntax which is able to do this kind of operation? I've never had a request like this before. Worst case scenario, I have to do it in php instead, but I think MYSQL is a lot more fun :p
I've built the schema on sqlfiddle, but I'm afraid that's all I've got.
You need a Tally table for the task like this. Create as much rows as needed in it.
Create table Tally(`N` int);
insert into Tally( `N`) values(1),(2),(3),(4),(5);
Then
(select `ID`, `WeightS`/`AmountS`, `Someothercolumnshere`
from Catches
join Tally on Catches.`AmountS` >= Tally.`N`
)
UNION ALL
(select `ID`, `WeightL`/`AmountL`, `Someothercolumnshere`
from Catches
join Tally on Catches.`AmountL` >= Tally.`N`
)
UNION ALL
(select `ID`, `WeightM`/`AmountM`, `Someothercolumnshere`
from Catches
join Tally on Catches.`AmountM` >= Tally.`N`
)

MySQL Query with LEFT JOIN where second table has a 2-Part Primary Key

I have 2 tables in a MySQL database (storeskus). The first is FBM_Orders and the second is IM_INV.
I am trying the query
SELECT `FBM_Orders`.`order-id`,`FBM_Orders`.`order-item-id`,`FBM_Orders`.`purchase-date`,
`FBM_Orders`.`promise-date`,`FBM_Orders`.`buyer-name`,`FBM_Orders`.`sku`,
`FBM_Orders`.`product-name`,`FBM_Orders`.`quantity-purchased`,
`FBM_Orders`.`recipient-name`,`IM_INV`.`LOC_ID`,`IM_INV`.`QTY_ON_HND`
FROM `FBM_Orders`
LEFT JOIN `IM_INV` ON `FBM_Orders`.`sku` = `IM_INV`.`ITEM_NO`
WHERE `FBM_Orders`.`quantity-to-ship` > 0
ORDER BY `FBM_Orders`.`purchase-date`, `IM_INV`.`LOC_ID` ASC;
Because the IM_INV table has a 2-part primary key: ITEM_NO & LOC_ID, I am getting 4 lines for each ITEM_NO with the QTY_ON_HND for each of the 4 locations (LOC_ID).
I am fairly new to SQL so I'm thrilled to have gotten this far, but how can I make it so that the result is a single line per ITEM_NO but with a column for each LOC_ID with its QTY_ON_HND?
Example:
My current result is
FBM_Order.sku FBM_Order.quantity-purchased IM_INV.LOC_ID QTY_ON_HND
'SCHO645256' 1 AF 2
'SCHO645256' 1 LO 2
'SCHO645256' 1 S 3
'SCHO645256' 1 SL 1
How can I change that to
FBM_Order.sku FBM_Order.quantity-purchased QTY_ON_HND_AF QTY_ON_HND_LO QTY_ON_HND_S QTY_ON_HND_SL
'SCHO645256' 1 2 2 3 1
?
Thanks!
You may load it as you already do and treat it inside your application, but if you really wanna make that inside your MySQL, try GROUP CONCAT and JSON as follows:
SELECT
GROUP_CONCAT(JSON_OBJECT(
'LOC_ID', IM_INV.LOC_ID,
'QTY_ON_HND', QTY_ON_HND
))
{another fields}
FROM `FBM_Orders`
LEFT JOIN `IM_INV` ON `FBM_Orders`.`sku` = `IM_INV`.`ITEM_NO`
WHERE `FBM_Orders`.`quantity-to-ship` > 0
GROUP BY `FBM_Orders`.`order-id`;
Note: JSON is just available for MySQL 5.7+ and may slow down your query a little bit. You're still gonna need convert your data to array inside your application. So it's half done inside your app and half inside your database.

Add blank row to MySQL query results based upon change in column value

If I have a typical MySQL query such as:
SELECT CommentNo, CreatedDate, CreatedTime, Text FROM Comment
The results will be displayed in a simple table. But is it possible to have MySQL format the output so that a blank line can be inserted into the results based upon a change in a column?
So if my query above returns the following by default:
CommentNo CreatedDate CreatedTime Text
1 2012-08-02 15:33:27 This.
2 2012-08-02 15:34:40 That.
3 2013-06-30 19:45:48 Something else.
4 2013-06-30 21:26:01 Nothing.
5 2013-06-30 21:26:43 Was.
6 2013-07-01 13:40:32 Hello.
7 2013-07-01 14:08:25 Goodbye.
Is it possible to insert a blank row upon change of column value for CreatedDate, so I get:
CommentNo CreatedDate CreatedTime Text
1 2012-08-02 15:33:27 This.
2 2012-08-02 15:34:40 That.
3 2013-06-30 19:45:48 Something else.
4 2013-06-30 21:26:01 Nothing.
5 2013-06-30 21:26:43 Was.
6 2013-07-01 13:40:32 Hello.
7 2013-07-01 14:08:25 Goodbye.
The above is not how data would be stored in the Comment table, it is an issue of formatting query output.
Or would it be better to use a BufferedWriter in Java?
First, let me say that this type of formatting should definitely be done at the application layer and not as a query.
That said, this seems like an amusing exercise, and it isn't so hard:
select CommentNo, CreatedDate, CreatedTime, Text
from (select c.*, CreatedDate as key1, 0 as ordering
from comment c
union all
select c2.*, c.CreatedDate, 1
from (select distinct CreatedDate from comment c) c left join
comment c2
on 1 = 0
) c
order by key1, ordering, id;
Note the use of the left join in the second subquery to bring in all the columns, so it matches the select * in the first subquery. However, getting rid of the last two columns still requires listing all of them.
I have adapted the code found at: enter link description here which can easily be extended to include a line when parsing the data to write the .csv file.
// Iinitialise FileWriter object.
fileWriter = new FileWriter(fileName);
// Iinitialise CSVPrinter object/ t.
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
// Create .csv file header.
csvFilePrinter.printRecord(FILE_HEADER);
// Write a new student object list to the .csv file.
for (Student student : students) {
ArrayList studentDataRecord = new ArrayList();
studentDataRecord.add(String.valueOf(student.getId()));
studentDataRecord.add(student.getFirstName());
studentDataRecord.add(student.getLastName());
studentDataRecord.add(student.getGender());
studentDataRecord.add(String.valueOf(student.getAge()));
csvFilePrinter.printRecord(studentDataRecord);
csvFilePrinter.println();
}
Although I have yet to add code to determine the change in the column value required but that is straightforward.

How to do this query against MySQL database table?

I was given a task to show the CPU usage trend as part of a building process which also do regression test.
Each individual test case run has a record in the table RegrCaseResult. The RegrCaseResult table looks something like this:
id projectName ProjectType returnCode startTime endTime totalMetrics
1 'first' 'someType' 16 'someTime' 'someOtherTime' 222
The RegrCaseResult.totalMetrics is a special key which links to another table called ThreadMetrics through ThreadMetrics.id.
Here is how ThreadMetrics will look like:
id componentType componentName cpuTime linkId
1 'Job Totals' 'Job Totals' 'totalTime' 34223
2 'parser1' 'parser1' 'time1' null
3 'parser2' 'generator1' 'time2' null
4 'generator1' 'generator1' 'time3' null
------------------------------------------------------
5 'Job Totals' 'Jot Totals' 'totalTime' 9899
...
The rows with the compnentName 'Job Totals' is what the totalMetrics from RegrCaseResult table will link to and the 'totalTime' is what I am really want to get given a certain projectType. The 'Job Totals' is actually a summation of the other records - in the above example, the summation of time1 through time3. The linkId at the end of table ThreadMetrics can link back to RegrCaseResult.id.
The requirements also states I should have a way to enforce the condition which only includes those projects which have a consistent return code during certain period. That's where my initial question comes from as follows:
I created the following simple table to show what I am trying to achieve:
id projectName returnCode
1 'first' 16
2 'second' 16
3 'third' 8
4 'first' 16
5 'second' 8
6 'first' 16
Basically I want to get all the projects which have a consistent returnCode no matter what the returnCode values are. In the above sample, I should only get one project which is "first". I think this would be simple but I am bad when it comes to database. Any help would be great.
I tried my best to make it clear. Hope I have achieved my goal.
Here is an easy way:
select projectname
from table t
group by projectname
having min(returncode) = max(returncode);
If the min() and max() values are the same, then all the values are the same (unless you have NULL values).
EDIT:
To keep 'third' out, you need some other rule, such as having more than one return code. So, you can do this:
select projectname
from table t
group by projectname
having min(returncode) = max(returncode) and count(*) > 1;
select projectName from projects
group by projectName having count(distinct(returnCode)) = 1)
This would also return projects which has only one entry.
How do you want to handle them?
Working example: http://www.sqlfiddle.com/#!2/e7338/8
This should do it:
SELECT COUNT(ProjectName) AS numCount, ProjectName FROM (
SELECT ProjectName FROM Foo
GROUP BY ProjectName, ReturnCode
) AS Inside
GROUP BY Inside.ProjectName
HAVING numCount = 1
This groups all the ProjectNames by their names and return codes, then selects those that only have a single return code listed.
SQLFiddle Link: http://sqlfiddle.com/#!2/c52b6/11/0
You can try something like this with Not Exists:
Select Distinct ProjectName
From Table A
Where Not Exists
(
Select 1
From Table B
Where B.ProjectName = A.ProjectName
And B.ReturnCode <> A.ReturnCode
)
I'm not sure exactly what you're selecting, so you can change the Select statement to what you need.

Complex - returning information that is not found in the database

I have a strange query to perform from a website. I have sets of arrays that contain pertinent ids from a many tables - 1 table per array. For example (the array name is the name of the table):
Array Set 1:
array "q": 1,2,3
array "u": 1,5
array "k": 7
Array Set 2:
array "t": 2,12
array "o": 8, 25
Array Set 3 (not really a set):
array "e": 5
I have another table, Alignment, which is not represented by the arrays. It performs a one to many relationship, allowing records from tables q,u, and k (array set 1, and recorded as relType/relID in the table) to be linked to records from t and o (array set 2, recorded as keyType/keyID) and e (array set 3, recorded as keyType/keyID). Example below:
Table: Alignment
id keyType keyID relType relID
1 e 5 q 1
2 o 8 q 1
3 o 8 u 1
4 t 2 q 2
5 t 2 k 7
6 t 12 q 1
So, in record 6, a record with an id of 12 from table t is being linked to a record with an id of 1 from table q.
I have to find missing links. The ideal state is that each of the ids from array set 1 have a record in the alignment table linking them to at least 1 record from array set 2. In the example, alignment record 1 does not count towards this goal, because it aligns a set 1 id to a set 3 id (instead of set 2).
Scanning the table, you can quickly see that there are some missing ids from array set 1: "q"-3 and "u"-5.
I've been doing this with script, by looping through each set 1 array and looking for a corresponding record, which generates a whole bunch of sql calls and really kills any page that calls this function.
Is there some way I could accomplish this in a single sql statement?
What would I like the results to look like (ideally):
recordset (consisting magically of data that didn't exist in the table):
relType | relID
q 3
u 5
However, I would be elated with even a binary type answer from the database - were all the proper ids found: true or false? (Though the missing records array is required for other functions, but at least I'd be able to choose between the fast and slow options).
Oh, MySQL 5.1.
User Damp gave me an excellent answer using a temporary table, a join, and an IS NULL statement. But it was before I added in the wrinkle that there was a third array set that needed to be excluded from the results, which also ruins the IS NULL part. I edited his sql statement to look like this:
SELECT *
FROM k2
LEFT JOIN alignment
USING ( relType, relID )
HAVING alignment.keyType IS NULL
OR alignment.keyType = "e"
I've also tried it with a Group By relID (i always thought that was a requirement of the HAVING clause). The problem is that my result set includes "q"-1, which is linked to all three types of records ("o","t", and "e"). I need this result excluded, but I'm not sure how.
Here's the sql I ended up with:
SELECT *
FROM k2
LEFT JOIN (
SELECT *
FROM alignment
WHERE keyType != 'e' and
(
(relType = 'q' AND relID IN ( 1, 2, 3 ))
OR
(relType = 'u' AND relID IN ( 1, 5 ))
OR
(relType = 'k' AND relID IN ( 7 ))
)
)A
USING ( relType, relID )
HAVING keyType Is Null
I have to dump the values for the IN qualifiers with script. The key was not to join to the alignment table directly.
You can try to go this route:
DROP TABLE IF EXISTS k2;
CREATE TEMPORARY TABLE k2 (relType varchar(10),relId int);
INSERT INTO k2 VALUES
('q',1),
('q',2),
('q',3),
('u',1),
('u',5),
('k',7);
SELECT * FROM k2
LEFT JOIN Alignment USING(relType,relId)
HAVING Alignment.keyType IS NULL
This should work well for small tables. Not sure about very large ones though...
EDIT
If you wanted to add a WHERE statement the query would be as follow
SELECT * FROM k2
LEFT JOIN Alignment USING(relType,relId)
WHERE Alignment.keyType != 'e'
HAVING Alignment.keyType IS NULL