SQL updating data with duplicate information - mysql

So i've been working on this query for awhile and for some reason whenever i run it, it tries to update certain fields that are already equal in both tables. For example take these two tables:
AlldataTable
first__last__age__number
K____ D __ 17____ 7
K____ D __ 15____ 7
X____ X___ 12 ____21
NewDataTable
first__last__age__number
K____ D __ 17____ 7
K____ D __ 15____ 7
X____ X___ 13 ____22
so the goal here is to have the age field for x x update to 13 and the number field to update to 22 when the query is executed. But when i execute the code it would say for example that it is going to update all three entries when just the third should be.
UPDATE AllDataTable
LEFT JOIN NewDataTable ON (AllDataTable.first = NewDataTable.first)
AND (AllDataTable.last = NewDataTable.last)
SET AllDataTable.age = NewDataTable.age
,AllDataTable.number = NewDataTable.number
WHERE (
AllDataTable.age <> NewDataTable.age
OR AllDataTable.number <> NewDataTable.number
)
AND AllDataTable.first = NewDataTable.first
AND AllDataTable.last = NewDataTable.last;

It's because of your duplicated values in first and last. The database is seeing three hits on the WHERE clause:
Alldata first row against NewData second row.
Alldata second row against NewData first row.
Alldata third row against NewData third row.
This gives the appearance of only one row changing, but you get an affected row count of three because the first and second rows actually swap all their contents in addition to the third row being updated.

Related

How to Find First Valid Row in SQL Based on Difference of Column Values

I am trying to find a reliable query which returns the first instance of an acceptable insert range.
Research:
some of the below links adress similar questions, but I could get none of them to work for me.
Find first available date, given a date range in SQL
Find closest date in SQL Server
MySQL difference between two rows of a SELECT Statement
How to find a gap in range in SQL
and more...
Objective Query Function:
InsertRange(1) = (StartRange(i) - EndRange(i-1)) > NewValue
Where InsertRange(1) is the value the query should return. In other words, this would be the first instance where the above condition is satisfied.
Table Structure:
Primary Key: StartRange
StartRange(i-1) < StartRange(i)
StartRange(i-1) + EndRange(i-1) < StartRange(i)
Example Dataset
Below is an example User table (3 columns), with a set range distribution. StartRanges are always ordered in a strictly ascending way, UserID are arbitrary strings, only the sequences of StartRange and EndRange matters:
StartRange EndRange UserID
312 6896 user0
7134 16268 user1
16877 22451 user2
23137 25142 user3
25955 28272 user4
28313 35172 user5
35593 38007 user6
38319 38495 user7
38565 45200 user8
46136 48007 user9
My current Query
I am trying to use this query at the moment:
SELECT t2.StartRange, t2.EndRange
FROM user AS t1, user AS t2
WHERE (t1.StartRange - t2.StartRange+1) > NewValue
ORDER BY t1.EndRange
LIMIT 1
Example Case
Given the table, if NewValue = 800, then the returned answer should be 23137. This means, the first available slot would be between user3 and user4 (with an actual slot size = 813):
InsertRange(1) = (StartRange(i) - EndRange(i-1)) > NewValue
InsertRange = (StartRange(6) - EndRange(5)) > NewValue
23137 = 25955 - 25142 > 800
More Comments
My query above seemed to be working for the special case where StartRanges where tightly packed (i.e. StartRange(i) = StartRange(i-1) + EndRange(i-1) + 1). This no longer works with a less tightly packed set of StartRanges
Keep in mind that SQL tables have no implicit row order. It seems fair to order your table by StartRange value, though.
We can start to solve this by writing a query to obtain each row paired with the row preceding it. In MySQL, it's hard to do this beautifully because it lacks the row numbering function.
This works (http://sqlfiddle.com/#!9/4437c0/7/0). It may have nasty performance because it generates O(n^2) intermediate rows. There's no row for user0; it can't be paired with any preceding row because there is none.
select MAX(a.StartRange) SA, MAX(a.EndRange) EA,
b.StartRange SB, b.EndRange EB , b.UserID
from user a
join user b ON a.EndRange <= b.StartRange
group by b.StartRange, b.EndRange, b.UserID
Then, you can use that as a subquery, and apply your conditions, which are
gap >= 800
first matching row (lowest StartRange value) ORDER BY SB
just one LIMIT 1
Here's the query (http://sqlfiddle.com/#!9/4437c0/11/0)
SELECT SB-EA Gap,
EA+1 Beginning_of_gap, SB-1 Ending_of_gap,
UserId UserID_after_gap
FROM (
select MAX(a.StartRange) SA, MAX(a.EndRange) EA,
b.StartRange SB, b.EndRange EB , b.UserID
from user a
join user b ON a.EndRange <= b.StartRange
group by b.StartRange, b.EndRange, b.UserID
) pairs
WHERE SB-EA >= 800
ORDER BY SB
LIMIT 1
Notice that you may actually want the smallest matching gap instead of the first matching gap. That's called best fit, rather than first fit. To get that you use ORDER BY SB-EA instead.
Edit: There is another way to use MySQL to join adjacent rows, that doesn't have the O(n^2) performance issue. It involves employing user variables to simulate a row_number() function. The query involved is a hairball (that's a technical term). It's described in the third alternative of the answer to this question. How do I pair rows together in MYSQL?

How do I make a google query that selects for row?

Example:
If my data set is
A B C
1 2 3
5 6 7
4 5 6
I could have "1", "5" and "4" show up by typing =query(A:C, select A).
I could have "1" and "4" show up by typing =query(A:C, select A where B < 6).
Lets say I wanted to query only entries that appeared after a certain row. In this case, row 3 is 4, 5, 6. So if I want only results that are row 3 or below, I could add a fourth column D somewhere, fill column D with =row(), and then have only **** show up by typing
=query(A:C, select A where D >= 3).
But I don't want to have to add a fourth column somewhere and fill it with the =row() formula. The query should be able to do this on its own.
Query parametres
try:
=QUERY(A:C,"select * offset 2",0)
offset parameter is zero base:
0 -- start from row 1
1 -- start from row 2
2 -- start from row 3
so on
You may find more usuful query tips here. Use special words: offset, limit, skipping. For example, to select only odd rows use:
=QUERY(A:C,"select * skipping 2",0)
Filter function
To have full control of rows you select, use this construction:
=filter(A:C,isodd(row(A:C))) -- only odd rows
=filter(A:C,row(A:C)=3) -- only 3-d row
=filter(A:C,row(A:C)>=3) -- all rows >= 3-d row
=query(filter(A:C,row(A:C)>=3),"select *") use filter + query
So, I had an issue like this, and here is what I did. I made a virtual column.
=query(A:C, select A where D >= 3) would then be something like
=query({A:C, ROW(A:C)}, "Select Col1 where Col4 >=3")
You have to make an array to query a column you do not want in your sheet. In doing so you can no longer use Column letters, so then A, B, C then becomes Col1, Col2, Col3 and so on.

Comparing counts across two tables

Given two tables...
tblEvents (fldEventID, fldAECap, fldDWCap, fldWSCap)
tblSignUps (fldSignUpID, fldEventID, fldPosition)
where a value of 1 in fldPosition relates to fldAECap, a value of 2 relates to fldDWCap, a value of 3 relates to fldWSCap; I need to prevent any new records from being inserted into tblSignUps if the count of fldPosition values is equal to each related CAP value. For instance, we may only have 3 allotted positions for fldAECAP and that position value occurs 3 times in fldPosition for that given EventID
The basic logic would come down to...
INSERT INTO tblSignUps
VALUES(NULL,12,3)
WHERE fldWSCap > COUNT(fldPosition(3))
How would I do this?
A way to write the required basic logic is this:
INSERT INTO tblSignUps
select NULL,12,3 from dual
where exists (
select * from tblEvents where
fldEventId = 12 and
fldWSCap > (select count(*) from tblSignUps where fldEventId = 12 and fldPosition = 3)
);
Obviously this works only for the field fldWSCap. The others two require a similar statement where the fld*Cap change according to the fldPosition value.

Deleting values in a coloumn except first 7 values

Trying to delete values in a column except the first 7
Column combine values= "0000396Memorial Hosp of Wm F & Gertrude F Jones A/K/A Jones Memorial Hosp"
I basically need just the first 7 values = 0000396
Can anyone please guide me to the right direction?
You can use the LEFT() string function to return the 7 leftmost characters:
UPDATE table SET column = LEFT(column, 7)
Assuming that I am understanding the question correctly, you can
UPDATE table SET column = SUBSTRING(column FROM 1 FOR 7);

Update multiple rows with multiple where clauses for each row. (update_batch can only handle 1 where clause) [duplicate]

This question already has an answer here:
Update multiple rows with multiple 'where' clauses for each individual row
(1 answer)
Closed 9 years ago.
I am trying to update my table like this:
Update MyTable
SET value = 1
WHERE game_id = 1,
x =-4,
y = 8
SET value = 2
WHERE game_id = 1,
x =-3,
y = 7
SET value = 3
WHERE game_id = 2,
x = 5,
y = 2
I can do a foreach() but that will send over 50 separate Queries which is very slow.
That's why I want it to be combined into 1 big Query.
( I do use an id for each row but the combination of game_id, x and y is what I use to Identify the row I need. )
The update_batch() function from codeIgniter described here:
Update batch with CodeIgniter
was helpful and almost perfect but it only allows for 1 single where clause, you cannot (as far as I understood and tried) enter an array with multiple where clauses.
I've also checked out this question:
MYSQL UPDATE SET on the Same Column but with multiple WHERE Clauses
But it only allows for multiple row updates containing only a single different WHERE clause and I need multiple WHERE clauses! :)
Anwsers can be in simple SQL or with the use of php (and CodeIgniter) or in a different way. I'd this problem to be solved in any possible way ;)
I can really use the advice/help! =D
EDIT:
Sorry the question wasn't clear enough, I just changed it!
It seems me you need the logical operator AND:
UPDATE MyTable
SET value = 1
WHERE game_id = 1
AND x = -4
AND y = 8
I'm not familiar with the multiple WHERE clauses how you have it laid out, why not just do
UPDATE MyTable
SET value = 1
WHERE game_id = 1
AND x = -4
AND y = 8