Add an extra column in select with '*' - mysql

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;

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.

Lowest unused ID from two combined tables

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

export first instance of database to the second instance

I have a project in ruby on rails 3.0.I have a database schema in pg. I have the instance of this project on 2 servers with respective databases.Now I have to shift everything to one server.So how do I export data from one database to the other?It can not be a literal export-import of databases bcos it has many tables with id and many to many relationships.So basically I need to append it so that there s no conflict for example
Databse 1 table 1 user
id Name
1 Josh
2 Rajn
4 Kush
Database table 1 user
id Name
1 Ram
2 Kevin
7 Don
So the new should be
Databse table 1 user
id Name
1 Ram
2 Kevin
7 Don
8 Josh
9 Rajn
10 Kush
and the join tables should have the new ids too
Not down the maximum value of the id field from all the target tables and add them as the offset values to the source id fields.
Ex:
migration.sql
SELECT #max_user_id := MAX(id) FROM users;
SELECT #max_comment_id := MAX(id) FROM comments;
# Then perform the following mysql commands in the target database:
INSERT INTO target.users(id, name) SELECT id + #max_user_id, name FROM source.users
INSERT INTO target.comments(id, comment, user_id) SELECT id + #max_comment_id, comment, user_id + #max_user_id FROM source.comments
Note that you cannot do the migration in

How to create a mysql join query with hierarchical data

I need to create a join query for hierarchical data across 2 tables. These tables can have unlimited amounts of data and their structures are as follows:
group_id group_name group_order
1 group 1 2
2 group 2 1
field_id field_name parent_group field_order
1 field 1 1 1
2 field 2 2 2
3 field 3 2 1
I am currently able to get the correct format of data using 2 select queries with the second query inside a loop created from the results of the first query on the groups table.
The structure of the data I require from the result is as follows:
-group 2
- field 3
- field 2
- group 1
- field 1
Is it possible to get these results from one mysql query? I have read through the mysql document on hierarchical data by I am confused about how to incorporate the join.
Thanks for looking
You shouldn't need to think about it in terms of hierarchical data, you should just be able to select your fields and join on your group information. Try something like:
SELECT *
FROM Fields AS F
INNER JOIN Groups AS G
ON G.group_id = F.parent_group
ORDER BY group_order, field_order
Then you will get each fields as a row with the applicable group, also in the correct group order. Your loop should be able to handle the display you need.
one method
something that may convince you change your db schema

MySQL - Select specific rows from multiple tables

I have a set of database tables named like:
site_1_details
site_2_details
site_3_details
...
site_420_details
all tables have the same fields, like:
ID | SETTING | VALUE
----------------------
1 name Site 1 Name
2 desc Site 1 Desc
3 email Site 1 Email...
...
(only the value fields are different)
How can I get a set of values from certain tables?
For example, I want to get the name & email values from sites 3,7 and 15. How could I do that with a SQL query?
SELECT 3 AS siteID, name, email
FROM site_3_details
UNION
SELECT 7 AS siteID, name, email
FROM site_7_details
UNION
SELECT 15 AS siteID, name,email
FROM site_15_details
This is a horribly bad design. Why couldn't you put a "siteID" field into a single table, which'd reduce the query to:
SELECT name, email
FROM site_details
WHERE siteID IN (3,7,15);
comment followup:
Ah well, then you just modify the individual queries:
SELECT 7 AS siteID, ID as fieldID, name AS fieldName
FROM site_7_details WHERE SETTING IN ('name', 'email')
UNION
....
Any reason you've designed the tables like this? Sounds like you're trying to implement your own database on TOP of a database engine which is already perfectly suited to doing this kind of relational data handling.