Lowest unused ID from two combined tables - mysql

I'm trying to write a SQL query to find the lowest available/unused ID from a column named internal that exists in two separate tables:
machines
machines_ignore
Data is processed from an external source, and we want to fetch data from all machines that are not in the machines_ignore table. The ignore table is just a manual table set up by us when we identify machines we don't want to analyze.
I've found scripts that work on a single table (like only the machines table), but as soon as I try to get it working when combining two tables.
Example
Table 1 (machines)
id
internal
1
1
2
2
3
3
4
5
5
6
Table 2 (machines_ignore)
internal
4
7
8
9
12
Expected result
Based on the example above, this query should output 10, 11, 13 etc.
Any ideas?

One solution is to combine the values from both tables then check if each value has next value in both tables using EXISTS:
SELECT x.internal + 1
FROM (
SELECT internal FROM machines
UNION
SELECT internal FROM machines_ignore
) AS x
WHERE NOT EXISTS (
SELECT 1 FROM machines WHERE internal = x.internal + 1
) AND NOT EXISTS (
SELECT 1 FROM machines_ignore WHERE internal = x.internal + 1
)
LIMIT 1

Related

How this query can be answered ? Select SUM(1) FROM table

select * from "Test"."EMP"
id
1
2
3
4
5
Select SUM(1) FROM "Test"."EMP";
Select SUM(2) FROM "Test"."EMP";
Select SUM(3) FROM "Test"."EMP";
why the output of these queries is?
5
10
15
And
I don't understand why they write table name like this "Test"."EMP"
your table has 5 records. the statement select 1 from test.emp returns 5 records with values as 1 for all 5 records.
id
1
1
1
1
1
This is because db engine simply returns 1 for each existing record without reading the contents of the cell. and same happens for select <any static value> from test.emp
same happens for 2 and 3
id
2
2
2
2
2
hence there are 5 records returned with the static values and sum of those values will be the product of static number passed in the select statement and total records in the table
additional fact: It is always recommended to perform count(1) than count(*) as it consumes less resource and hence less load on the server
I don't think it's "Test"."EMP" with double quotes.. it's probably `Test`.`EMP` with backticks instead. The definition means its database_name.table_name. This is the recommended format to get the correct table_name from database_name; in this case, you're specifically making the syntax to query from `Test`.`EMP`. Read more about identifier qualifiers.
As for SUM(x), the x get's repeated according to the rows present in the table. So SUM(1) on 5 rows is 1+1+1+1+1, SUM(2) on 5 rows is 2+2+2+2+2, and so on.

Merge two colums result as single colum data using Mysql query

I want to get all record data as single column using SQL query.User Table
user_id username parent_id
10 user1 5
12 user2 3
14 user3 2
.. .. ..
get all users with parent id as single column
Need results as below (get data from user_id & parent_id)
users OR users
10 10
12 5
14 12
5 3
3 14
2 2
.. ..
Here just need list of user sequence is not important.Is this possible in sql query? is there any SQL function for that?
This will do the trick.
SELECT user_id AS users FROM Users
UNION
SELECT parent_id AS users FROM Users
Use UNION ALL if you need duplicate values (such as in the parent_id column) to return as individual rows. Make sure the alias is the same for both halves to get it to return in a single column. This will return unordered, so rerunning the query might not always give the same result (meaning same data in same order), but it will always give the same data.

Add an extra column in select with '*'

I am writing a query sql for a data migration job to fetch data and send from mysql-server a to mysql-server b.
Server a has different databases, representing different game channel, and each database has a table tablex, they have same table name and same schema:
uid level
123 3
211 5
While in server b there is only one table tablex to receive tablex of all databases and it has one more column - channel
channel uid level
1 123 3
1 211 5
2 355 2
I can parse channel number from db name via python but I need to put this constant in the sql and since there are many tables, I cannot fix the columns. So just want to make sure is there any way to do this like:
select 1,* from xxx.yyy
You could try adding alias and table name
select 1 as my_col, yyy.* from xxx.yyy
or using string
select cast('1' as unsigned) , yyy.* from xxx.yyy
Yes, you can wrap the query into another SELECT and add the columns, e.g.:
SELECT A.*, '1'
FROM (
SELECT *
FROM your_table
) A;

MySql: adding columns dynamically, as many as rows in another table

Transport table
id name
1 T1
2 T2
Pallets table
id name
1 P1
2 P2
Transport Pallet Capacity table
id transport_id pallet_id capacity
1 1 1 10
2 1 2 null
3 2 1 20
4 2 2 24
How to generate table like this:
id transport_id pallet_id_1_capacity pallet_id_2_capacity
1 1 10 null
2 2 20 24
Problem: pallets and transports can be added, so, neither quantity is known in advance.
For example, manager adds another pallet type and 'pallet_id_3_capacity' column should be generated (and can show null if no capacity data is yet available).
Another manager can fill 'transport pallet capacity' table later when notified.
Is there a way to build sql in mysql that will care about the above: specifically - dynamic number of pallets?
The SQL select-list must be fixed at the time you write the query. You can't make SQL that auto-expands its columns based on the data it finds.
But your request is common, it's called a pivot-table or a crosstab table.
The only solution is to do this in multiple steps:
Query to discover the distinct pallet ids.
Use application code to build a dynamic SQL query with as many columns as distinct pallet id values found in the first query.
Run the resulting dynamic SQL query.
This is true for all SQL databases, not just MySQL.
See MySQL pivot row into dynamic number of columns for a highly-voted solution for producing a pivot-table query in MySQL.
I am not voting your question as a duplicate of that question, because your query also involves transport_id, which will make the query solution a bit different. But reading about other pivot-table solutions should get you started.

Insert date from two tables in MySql

Consider the following two table
Table A:
id int auto_increment
with 2 rows of data.
id
1
2
Table B:
id auto_increment
aid reference to A.id
with 3 rows of data
id aid
1 1
2 2
3 2
If now the table A has been inserted 2 rows to
Table A:
id
1
2
3
4
So, how to write the insert statement so that it can insert to table B as result
Table B:
id aid
1 1
2 2
3 2
4 3
5 4
6 4
7 5
8 6
9 6
The question is for study only. I know there are many ways to do it, but I am just wondering if it can be done by using sql or just in mysql?
UPDATE
Sorry, the question since to be unclear. Let me state it in clear.
The data in table B has relation to table A. B.aid = A.id
The new data in table A, which is A.id, are in sequence, also has relation to the first two id. That means 1 and 3 with the same meaning, 2 and 4 also.
In the insertion of table B, it should consider both 1. and 2. That means, since with one aid=1 and two aid=2, then the data that needs to insert into table B is one aid=3, two aid=4, one aid=5 and two aid=6.
The question: This can be done easily with programming, however, I just wondering can 3. be done in a insert statement with out programming in Mysql?
Assuming you are inserting the ID(s) into Table A,
INSERT INTO [Table B](aid) VALUES ([THE ID])
Where this will insert the specified ID, in the aid column in Table B.
Otherwise if you want to match up the values you can use NOT IN to detect values that are not like, then you must insert these values,
This will select all IDs that do not exist in Table B:
SELECT id
FROM [Table A]
WHERE [Table A].id Not In (SELECT aid FROM [Table B])
...then later (depending what you're using it in, PHP? Then fletch the data (should look something like this))...
while($row = mysqli_fetch_assoc(...))
{
INSERT INTO [Table B](aid) VALUES($row['aid'])
}