MySQL - Select rows from multiple tables based on the tables' names - mysql

I'm currently trying to set up a simple database via MySQL.
To sum up, I have a first Table 'A' containing a column 'name' and a column 'value'. Next I have a bunch of other tables 'name1', 'name2'... 'nameN' whose name actually correspond to one of the name stored in A.name.
Now I would want to select stuff from those tables based on a desired value for A.value. Something like that :
SELECT * FROM ( SELECT name from A WHERE A.value = desired_value );
Thanks a lot for your help !

Related

What is the new table name after joining two tables in sql?

I read the previous posts but I couldn't find one that answered my question.
What would be the name of the table that is made by joining two tables? The reason why I need the name is because I would like to change the column name of the new table using the ALTER TABLE (Table name) RENAME COLUMN (A) to (B). If there is no specified name, how can I name the new table?
ex
SELECT
client_id
,last_name
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name
FROM BUSINESS_CLIENT;
I would like to rename the column to last_name/bus_name instead of last_name
In the case of a query what temporal table a query might create internally is not relevant, because you a making a query and getting data back, it doesn't stay in the database as a table, there is no such table. Unless we make it.
if You want TSQL to change a column name it would affect your union query and I base my answer on Your
'I would like to rename the column to last_name/bus_name instead of last_name'
And think this is what you're looking for. Please correct me if it isn't.
In generic SQL what we're doing is putting a label on both projections that are to be displayed in the same column
SELECT
client_id
,last_name [last_name/ bus_name]
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name [last_name/ bus_name]
FROM BUSINESS_CLIENT;
update, in MySQL notation uses AS and quotes instead of angle brackets
SELECT
client_id
,last_name as "last_name/ bus_name"
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name as "last_name/ bus_name"
FROM BUSINESS_CLIENT;

combobox with data from several fields in access

I have created a table with several columns and a related form. In that form, I have created a combobox to show the columns of the table in differents rows in combobox.
I have 3 fields in my table: product1, product2 and product3 for a same order I have named with a number. When I create the combobox, values show in 3 differents columns in the same row so I can just select the data from the first row and column. But what I need is the field's data displayed in different rows instead showing data in different columns.
I researched in forums, and I read It can be solved with a join query and selecting that in row source in combobox menu, but I have tried it and I got the same result. I don´t know what am I doing wrong. I'd appreciate your help.
Thanks in advance.
You have to pivot the results
this is an example of how to restructure using sub queries bad data designs
SELECT *
FROM
(
SELECT QualifierID -- <--- this is a unique identifier
,
(
SELECT QualifierText
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS QualifierText
,
(
SELECT [ExampleText]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS ExampleText
,
(
SELECT [WhenModified]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS WhenModified
FROM [QDB].[dbo].[Qualifier] q -- this is a table alias
) z -- this is the alias fo the subquery combining everything together.

SQL - How to replace partial text in a text field that contains matching data from list

I have a question about replace function (or anything similar to that).
I have a query that returns more than 1500 results and one of the columns is a 'BODY' column which includes inside sql query.
The columns of the returned table are: Id,name, date, title, body.
In the body column i have sql query such as:
"select * from tbl.student s inner join tbl.teacher t on s.id = t.sId
where title like '%Joni%'"
And than I have a separate list of 60 tables which some of them for example: tbl.student, tbl.teacher, tbl.class, tbl.school and etc.
I want to run on all 1500 results that iv'e got, look for each result if body field contains one of the tables that in the list (tbl.student, tbl.teacher, tbl.class, tbl.school ...
) and if so i want to replace tbl. to abc.
For example, if one of the results contains in its body field tbl.student I want to replace it to abc.student
What will be the most effective way to do that?
Thanks!

Merging every 2 rows of a MySQL database table in a single row

I have a database table that contains person's fingerprints (template column here), each person can introduce 2 fingers, so the same person must have 2 records.
Here is the table :
Person with ID '275' have 2 records, each for a single finger.
Now I am using Talend to create a table so I can merge every two fingerprints in a single one, I mean row n°37 and 38 will be in single row and template column will be concatenated to have only one person_id
You can try something like this:
INSERT INTO newtable (ids, person_id_integer)
SELECT CONCAT(finger1.id, "|", finger2.id), finger1.person_id_integer
FROM oldtable finger1, oldtable finger2
where finger1.person_id_integer = finger2.person_id_integer
and finger1.id <> finger2.id
Of course, depnds on how you want to store the new data
:)
I found a solution :
I added the same table twice as an input and output like shown in this photo and first fetched it with this query :
select FP_ID, FP_DEVICE, group_concat(FP_FINGER_PRINT SEPARATOR ''),FP_EMPLOYEE_ID
from t_finger_print GROUP BY FP_EMPLOYEE_ID;

What's the mysql syntax to pull data on the fly

I have a table with a bunch of orders... one of the columns is order_status. The data in that column ranges from 1 to 5. Each number relates to a name, which is stored in another table that relates that number to the respective name.
SELECT order_id , order_status FROM tablename1
The above would just return the numbers 1,2,3,4,5 for order status. How can i query within the query on the fly to replace these numbers with their respective names.
Also, what's the term used to describe this. I'd Google it if i knew what the appropriate term was.
Each number relates to a name, which is stored in another table that
relates that number to the respective name.
JOIN it with the other table:
SELECT
t.order_id,
s.StatusName
FROM tablename1 AS t
INNER JOIN the statusesTable AS s ON t.order_status = s.status_id;