I don't know much about MySQL but I have to do sth. with this nice thing. :)
I have 1 Table with 2 ID Fields in it (ID and REFID).
I only want to select the ID's which are not between other ID to REFID.
Row 1: ID = 1 and REFID = 15
Row 2: ID = 17 and REFID = 20
Row 3: ID = 19 and REFID = 25
I only want Row 1 and 2 in my result.
because Row 3 is crossing Row 2.
Sorry for my bad explanation,
I hope you know what I want to know. :)
This is based on the general pattern for finding a row in a table that doesn't match another table, as in Return row only if value doesn't exist. But in this case the two tables are the same table, and the match is by checking for id being between id and RefID in the other row.
SELECT t1.*
FROM yourTable AS t1
LEFT JOIN yourTable AS t2 ON t1.id > t2.id AND t1.id < t2.RefID
WHERE t2.id IS NULL
DEMO
For this, you should use MySql procedures. You can find the tutorial here.
Are you doing this with PHP?
If so maybe something like this...
- create a query to return the desired columns of the table in an array ORDER by id
- then create a loop to start with get the records starting with the id equal to the lowest id number
- and repeat the loop until you have retrieved the record with the highest id number you desire.
or use WHERE in the query to specify the id range you want returned.
One of these approaches should get you there... I think :-)
Related
I have two tables, with 2 PKs. Table 1 has 478 records. Field 1 is a unique ID for that table only. Table 1 field 2 is a ID (shared with table 2) and 3rd field is a category field. IDs from field 2 can be repeated within a table, but I cannot have ID+category twice.
I have a 2nd table, that contains 757 records. It has a ID column and a category column (such as table1) and I want to know which records from table 1 are included on table 2. By the moment I am just checking which IDs are included in both tables (I want to clean up the database so I can use an AND query to obtain ID + category)
My SQL query does not return the desired result. When I do
SELECT DISTINCT(table1.field1) FROM table1, table2 WHERE table1.ID = table2.ID;
I get all the results that do match, but, when I do the opposite
SELECT table1.field1 FROM table1, table2 WHERE table1.ID != table2.ID;
SQL gives all the rows from table 1, when, the expected outcome would be
total rows from table 1 - IDs that do match with the ones at table 2
I've tried to invert the order in which the query is displayed as:
SELECT table1.field1 FROM table1, table2 WHERE table2.ID != table1.ID;
But then a loop occurs and I get 36000+ results which is, of course, impossible (I imagine that checking a bigger record table against a smaller one makes the small one loop over and over, and seeing that I get the full table all the time, the loop is Xtimes478, hence the 36000+ results).
I have checked this matched/unmatched query using R (just for testing) and I got 170 matches (that I can obtain in SQL) and 308 "not coincident" results (170+308=478, so I imagine it makes sense even if I am using R instead of a proper relational database system)
How can I search for unmatched IDs in a query rather than checking for matched ones and substracting from total? How to get the 308 records that do not match?
If you want values in table 1 that are not in table 2, then use not exists or something similar:
select t1.*
from table1 t1
where not exists (select 1 from table2 where t2.id = t1.id);
I have two tables that are very similar. For example, let's say that each row has two ID numbers, and a data value. The first ID number may occur once, twice, or not be included, and the second ID number is either 1 or -1. The data value is not important, but for the sake of this example, we'll say it's an integer. For each pair of ID numbers, there can only be one data value, so if I have a data point where the ID's are 10 and 1, there won't be another 10 and 1 row with a different data value. Similarly, in the other table, the data point with ID's 10 and 1 will be the same as in the first table. I want to be able to select the rows that exist in both tables for the sake of changing the data value in all of the rows that are in both. My command for MySQL so far is as follows:
SELECT DISTINCT * FROM schema.table1
WHERE EXISTS (SELECT * from schema.table1
WHERE schema.table1.ID1 = schema.table2.ID1
and schema.table1.ID2 = schema.table2.ID2);
I want to be able to have this code select all the rows in table1 that are also in table2, but allow me to edit table1 values.
I understand that by creating a union of the two tables, I can see the rows that exist in both tables, but would this allow me to make changes to the actual data values if I changed the values in the merged set? For example, if I did:
SELECT DISTINCT * FROM schema.table1 inner join schema.table2
WHERE schema.table1.ID1 = schema.table2.ID1
schema.table1.ID2 = schema.table2.ID2;
If I call UPDATE on the rows that I get from this query, would the actual values in table1/table2 be changed or is this union just created in dynamic memory and I would just be changing values that get deleted when the query is over?
Update as follows:
UPDATE table1 SET data = whateverupdate
WHERE ID1 IN (SELECT ID1 from schema.table1
WHERE schema.table1.ID1 = schema.table2.ID1
and schema.table1.ID2 = schema.table2.ID2);
In your inner select statement, you cannot do a select * you'll have to select a particular column. This should work because your inner select finds the row in question and feeds it to your update statement. That being said, your inner select has to return the right row you need, else, the wrong row will be updated. Hope this helps.
I am playing around with ms-access (MS-Office Professional Plus 2013) trying to figure out if I have duplicate rows before I merge one table into another table. I want to collect the rows that are duplicates and give an error with the duplicates before the merge happens. I have two scenarios to cover. The first scenario is duplicates on a single column. The second scenario is duplicates on two columns. Any help on the first scenario would be appreciated.
Scenario 1:
The two tables have the exact same column structure so to keep it simple I will use the following table structure. ( I simply added two tables inside access and run the query to figure out the correct syntax.)
Duplicates based upon one column:
Table1 Table2
ID ID
1 1
2 3
Running the query:
Select ID from Table1
Union ALL
Select ID from Table2
group by ID having count(*) > 1
The result set is always the records from the first select statement. In other words it always returns Id=1 and Id=2. If you change Table1 to Table2 the result set is always from table2. If I change "Union all" to union same results. I tried changing the ID column names as well as change the type to be number instead of auto. Any idea what am I doing wrong?
Scenario 2: I know what the value should be in the second column so it is hard-coded. I added this here to show access appears to work as expected in this scenario but not in scenario 1.
Duplicates based upon two columns:
Table1 Table2
ID Field1 ID Field1
1 abc 1 abc
2 bcd 3 abc
Running the query below works as expected. The row with ID=1 is only returned.
select ID, Field1 from Table1 where Field1 = 'abc'
union all
select ID, Field1 from Table2 where Field1 = 'abc'
group by ID, Field1 having count(*) > 1
The GROUP BY is only being applied to the second table. You need to do the UNION ALL first, and then the GROUP BY and HAVING on a SELECT from the combined results.
Not Access specific, but something like this works:
SELECT id FROM
(
SELECT id FROM a
UNION ALL
SELECT id FROM b
) AS c
GROUP BY id HAVING COUNT(*) > 1
My preferred way to do things like that is to use the build in Query Wizard:
Query Wizard, Find Duplicates Query Wizard
Let Access create the SQL statement for you and then you can modify it and/or move it into code.
I want to fetch latest entries in a table that is containing more than 1,000,000 entries. I am using this query for an instance
SELECT id FROM tablea WHERE flag = "N" ORDER BY id LIMIT 510045,200;
and it gives me entries starting from 510045 and ending at 510245. Can MYSQL have something where I can get entries starting from 510245 to 510045. I mean fetching the data from the last and I don't want to fetch only 200 entries.
You should ORDER BY desc and, if you want, LIMIT for define how many entries you want.
Example:
SELECT id FROM tablea WHERE flag = "N" ORDER BY id DESC;
-- this will help to find the last entries
But if you want to have the latest entries that you didn't get in last query, you should always hold the value of the last ID, and use it as reference to next check.
Example (Supposing the last ID of the last query execution was 55304):
SELECT id FROM tablea WHERE flag = "N" WHERE id > 55304 ORDER BY id DESC;
If what you want is rows where the id is greater than 510245 just use the where condition
Select * FROM table WHERE flag = 'n' AND id > 510245
This should do it
As i understand your requirement . you may try it.
Select * FROM table WHERE flag = 'N' AND id > 510245 ORDER BY id
One more thing here is
The version i was working on was not supporting subquery containing LIMIT. So, #strawberry Thanks for giving me the hint to solve the question. But I used this sub query as inner join table(explained below)
SELECT id FROM tablea AS T1
INNER JOIN (SELECT id FROM tablea WHERE flag = "N" ORDER BY id LIMIT 510045,200) AS T2
WHERE T2._id = T1._id ORDER BY T2._id DESC;
This gave me the required results. Thanks everyone for your help !!
I'm trying to grab the latest ID from a duplicate record within my table, without using a timestamp to check.
SELECT *
FROM `table`
WHERE `title` = "bananas"
-
table
id title
-- -----
1 bananas
2 apples
3 bananas
Ideally, I want to grab the ID 3
I'm slightly confused by the SELECT in your example, but hopefully you will be able to piece this out from my example.
If you want to return the latest row, you can simply use a MAX() function
SELECT MAX(id) FROM TABLE
Though I definitely recommend trying to determine what makes that row the "latest". If its just because it has the highest column [id], you may want to consider what happens down the road. What if you want to combine two databases that use the same data? Going off the [id] column might not be the best decision. If you can, I suggest an [LastUpdated] or [Added] datestamp column to your design.
im assuming the id's are autoincremented,
you can count how many rows you have, store that in a variable and then set the WHERE= clause to check for said variable that stores how many rows you have.
BUT this is a hack solution because if you delete a row and the ID is not decremented you can end up skipping an id.
select max(a.id) from mydb.myTable a join mydb.myTable b on a.id <> b.id and a.title=b.title;