SQL one row equals to two merged rows - java

Hi I'm doing project in Java and i have two tables in SQL and i want to link data between them.
Here is my problem, the first table have 2 columns named name and surname , and the second table have a row namesurname (there are other columns in each table) where name and surname are connected.
Table 1 :
name - Example
surname - Something
Table 2:
namesurname - Example Something
How can i make a link between these two tables to display all data from Table 2. based on inserted name and surname in Table 1.

Have you tried something like
select * from 1.table
join 2.table on [2.table].namesurname = [1.table].name+" "+[1.table].surname

Related

Java - Merge data from 2 tables in different data source

I have a use case, where I have to merge data from 2 Tables (which are in different data sources).
Consider that each table has employee data (First name, last name, phoneNumber). For each employee, I will have to merge the phoneNumber data from both the Tables and other data will remain the same as in Table 1
If employee data is present in both Table 1 and Table 2, phoneNumber data will be merged as a comma separated values and other data will be sourced from Table 1
If employee is present only in Table 1, then entire data will be sourced from Table 1
If employee is present only in Table 2, then entire data will be sourced from Table 2
These Tables has about 40 lack rows of data each. Close to 5GB.
What is the best approach to do this in Java? My concern is if I pull the data from these 2 tables into Java cache, i will still have to loop thought the entire table 2 to see if an employee is present there as well.
Or will a python script be better?
Table 1
EmployeeID
FirstName
LastName
PhoneNumber
EM01
Jhon
Doe
12345
EM02
Dave
Joe
34567
Table 2:
EmployeeID
FirstName
LastName
PhoneNumber
EM01
Jhon
Doe
89000
EM03
Gabe
Mai
45678
Table 3 (After merging the phone numbers):
EmployeeID
FirstName
LastName
PhoneNumber
EM01
Jhon
Doe
12345,89000
EM02
Dave
Joe
34567
EM03
Gabe
Mai
45678
You can easily do this as a SQL query.
Basically, you want a full join, but -- alas -- MySQL doesn't support that.
So, one method is:
select t1.EmployeeID, t1.FirstName, t1.LastName
concat_ws(',', t1.PhoneNumber, t2.PhoneNumber) as PhoneNumber
from table1 t1 left join
table2 t2
on t1.EmployeeID = t2.EmployeeID
union all
select t2.EmployeeID, t2.FirstName, t2.LastName, t2.PhoneNumber
from table2 t2
table1 t1 left join
on t1.EmployeeID = t1.EmployeeID
where t1.EmployeeID is null;
That is, get all the rows for the employees in table1. Then add in the additional rows from table2. For the first part, concat_ws() is convenient for combining the phone numbers.

Writing data to tables with relationship in SQL Server

I have two different tables with multiple columns. Like the following
Table1
ID CUSTOMER PRODUCT DETAILS
21 joe phone
22 doe shoe
23 john cup
Table2
PRODUCT ID COST ID
1 9000 21
2 600 22
3 30 23
ID in table1 is primary index and PRODUCT ID in table2 is the primary index. The common link between these two tables is ID. I used the following query to read the values.
String query = "SELECT * FROM Table1 c1 INNER JOIN Table2 c2 ON c1.ID = c2.ID";
But my problem is when I am adding new row, I use the following queries for it.
String query1 = "INSERT INTO Table1(CUSTOMER) VALUES ('" + alex + "') ";
String query2 = "INSERT INTO Table2(COST) VALUES ('" + 500 + "') ";
Since ID is not the primary index for Table2, I get a NULL value in ID. When I try to read again, since ID are not matched the I cannot reload the saved data.
Edit
This is what I have tried based on the explanation given here,
String foreignKey = "ALTER TABLE Table2 ADD FOREIGN KEY (ID) REFERENCES Table1(ID)";
Still the column ID is null in table2. Do I need combine the queries in some sort to acheive the result ? Because I just execute queries one by one as follows.
Statement stmt = connect.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.executeUpdate(foreignKey);
General idea here - you need to get autogenerated id from first "insert" to Table1 and set "ID" at Table2 explicitly with this id in the second "insert". You can see how to do that here for example.
Foreign key in your second table - is just a constraint, that helps to prevent data's inconsistency. Its main purpose - is to give some message to you when you're trying to put some data with foreign key that doesn't exist in primary table. Also it can help (if you set this while creating foreign key) to remove from Table2 rows linked on foreign key with Table1 when you delete row at Table1.
And you shouldn't call stmt.executeUpdate(foreignKey); each time you insert data to Table1, Table2. It should be done once when you describe the structure of your database.

JPA method signature for binary match in mariaDB

In my table of mariaDB database, I have a column name and It is going to store a person's name in any language.
There are 2 names änkür and ankur in table.
If I run below query, it is returning 2 rows
select * from table_name where name ='ankur';
As I want to return single result I changed my query like:
select * from table_name where name = BINARY 'ankur';
What is the JPA method signature or alternative solution for the above query?

How do make columnname from one table as a tablename in sql join

i have 2 tables . column of one table is part of tablename to others , i want to make join in between these tables something like below.
select ar.id,ar.type from Art ar ,CONCAT('MOK', ar.type) mo where mo.id= ar.id
There is same id present in both table but mo.id does not exist is the exception i m getting.thnx for your time. name of second table is MOKdance where dance =ar.type

How select column names ,column data type, key column of a table in mysql

How select column names ,column data type, key column of a table in mysql by passing the table name.
I am using mysql 5.5
Pick the info you need from
SELECT *
FROM information_schema.columns
or
SELECT *
FROM information_schema.tables
Try this:
Get structure of table
DESCRIBE table;
SELECT * FROM information_schema.tables where table_name = 'table';
Get a list of the tables in your database.
SHOW TABLES;
Whole database structure
mysqldump database_name
Select database,
execute command using desc or SHOW FIELDS FROM.
Code:
`use testDB;`
``desc `testDB`.`images`;``
-- or
`SHOW FIELDS FROM images;`
See my previous answer How to show mysql table columns data type?, it solves your problem.
The results are shown in attached image file.

Categories