SQL select with complex insert - mysql

I am trying to concatenate 2 tables into 1 table with 1 mysql command. I also want to concatenate a file path into one path in the new table. Any information would be helpful. Thank you.
Something like this :
INSERT INTO table3 VALUES (Location) SELECT "A.Loc_Path B._FilePath" FROM Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID
I would like to take Table 1 & 2 and create Table 3
Table 1
| ID | _FilePath | _Loc_ID |
| 1 | 001\yay\txt.html | 1 |
| 1 | 002\yay\txt.php | 2 |
Table 2
| _Loc_ID | Loc_Path |
| 1 | D:\documents\test\ |
| 2 | C:\Temp\test\ |
Table 3
| Id | Location |
| 1 | D:\documents\test\001\yay\txt.html |
| 2 | C:\Temp\test\002\yay\txt.php |

Use concat
INSERT INTO table3 (Location) SELECT concat (A.Loc_Path , B._FilePath)
FROM Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID

what you need is to use the concat function to concatenate the two columns:
INSERT INTO table3 VALUES (Location) SELECT CONCAT(A.Loc_Path, B._FilePath) FROM
Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID

Related

MySQL - how to find similar data in two mysql tables?

I have two table like this :
table1
| cat | id |
|----- |------|
| 110** | 12 |
| 110** | 18 |
| 110** | 13 |
----------------
table2
| cat | qty |
|----- |------|
| 11012 | 2 |
| 11017 | 8 |
| 11016 | 1 |
----------------
result
| cat |
|----- |
| 11012 |
---------
can I combine cat,id columns in table1 (like 11012) to get the same value in cat column in table2 (like 11012)?
I tried a query like this:
SELECT a.cat,a.id FROM table1 a JOIN table2 b ON a.cat LIKE CONCAT('%', b.cat, '%')
but I didn't get the results I expected.
SELECT *
FROM table1 t1
JOIN table2 t2 ON CONCAT(TRIM(TRAILING '*' FROM t1.cat), id) = t2.cat
Logic: remove trailing asterisks from cat, concatenate id, then compare.
fiddle
If I understand question correctly, you should do
SELECT a.cat,a.id FROM table1 a JOIN table2 b ON LEFT(a.cat,3) = LEFT(b.cat, 3)

Update one MySQL table with values from another (Datatype of the original table is JSOn)

I'm trying to update one MySQL table based on information from another.
The original table where I would like to compare the values to the other table to be updated has a JSON object in it.
Here is what my tables look like
Original Table
Table1
id | programme
------------
1 | ["22","34"]
2 | ["10","11","12","13","14","15","17","18","19","20"]
Table to be updated
Table2
id | programme_id | table1_id
-----------------------------
1 | 22 |
2 | 18 |
3 | 12 |
UPDATE table2
INNER JOIN table1 USING (programme_id)
SET table2.table1_id = table1.id
Here is the expected output:
id | programme_id | table1_id
-----------------------------
1 | 22 | 1
2 | 18 | 2
3 | 12 | 2
set sql_safe_updates = 0;
update table2
inner join table1
on JSON_SEARCH(table1.programme,'one',table2.programme_id) is not null
set table2.table1_id = table1.id;

How to write such query?

How do you programming in SQL, I don't understand nothing in it. :)
Table1:
+-----------+----------+------------+
| id | key | page |
+-----------+----------+------------+
| 1 | A | 123 |
| 2 | B | 456 |
| 3 | C | 777 |
+-----------+----------+------------+
Table2:
+-----------+----------+------------+
| id | key | page |
+-----------+----------+------------+
| 1 | A | 123 |
| 2 | B | 456 |
| 3 | C | 111 |
+-----------+----------+------------+
I have 2 tables. We can see that they have fields with the same 'key' (A, B, C). How can I filter 'page' column's value which are differ in Table1 and Table2.
So, in page column:
123=123, 456=456, 777<>111 ==> I want such table as Result:
NEW Table:
+-----------+---------------------+---------------------+
| id | id & page_table1 | id & page_table2 |
+-----------+---------------------+---------------------+
| 1 | 3 777 | 3 111 |
+-----------+---------------------+---------------------+
#LukazSzozda's comment seems to be the right answer. But if you actually want exactly those three comments in the result, you can concatenate them with the concat function, like this
select a.id,
concat(a.id, ' ', a.page)
concat(b.id, ' ', b.page),
from Table1 a
join Table2 b
on a.id = b.id
and a.key = b.key
where a.page != b.page
Your query is not "such difficult". First you need to join both tables on key column and then filter using page column:
SELECT *
FROM tab1
JOIN tab2 ON tab1.`key` = tab2.`key`
WHERE tab1.page <> tab2.page;
If you want new table with new ID, you need to have create table. I recommend different format
CREATE TABLE newTable (
id INT AUTO_INCREMENT,
oldId int NOT NULL,
tbl1Page int,
tbl2Page int,
PRIMARY KEY (id))
INSERT INTO newTable (oldId, tbl1Page, tbl2Page)
SELECT t1.key, t1.Page, t2.Page
FROM tab1 t1 INNER JOIN
tab2 t2 ON t1.key = t2.key
WHERE t1.page <> t2.page
I don't recommend combining ID and Page (or any other 2 data pieces) into one column. You can always combine data when you query it. Read about data normalization
Select concat(f1, ' - ', f2) as combinedData . . .

Joining 2 sql table into one table

Im trying to link a list of ID to a list of date in SQL.
This is the data i have both in different table.
| Date | ID |
|2017-12-25| 1 |
|2017-12-26| 2 |
|2017-12-27| 3 |
I will like to merge this into
| Date | ID |
|2017-12-25| 1 |
|2017-12-25| 2 |
|2017-12-25| 3 |
|2017-12-26| 1 |
|2017-12-26| 2 |
|2017-12-26| 3 |
|2017-12-27| 1 |
|2017-12-27| 2 |
|2017-12-27| 3 |
I would use CROSS JOIN for SQL Server
select a.[date], t.id from table t
cross join
(
select * from table
)a
You'll probably want to MERGE
MERGE table1 <alias1>
USING table2 <alias2>
ON <alias1>.ID = <alias2>.ID
WHEN NOT MATCHED BY TARGET THEN
INSERT (ID, Date)
VALUES (<alias1>.ID, <alias1>.Date);
SELECT * FROM table1;

MySQL query to select group of IDs from one table, depending on query on a second table

I am sure this would be easy to google if I knew the right words to use, but I've tried and not come up with anything: apologies if this is a common question on SO.
I have one table which lists a set of records which can be one of 4 types.
table_1:
+-------+------------+------+
| id | value | type |
+-------+------------+------+
| 1 | x | 1 |
| 2 | y | 1 |
| 3 | z | 2 |
| 4 | a | 3 |
+-------+------------+------+
I have another table which references the id of this table and stores data
table_2:
+-------+------------+------+
| id | table_1_id |value |
+-------+------------+------+
| 1 | 4 | A |
| 2 | 2 | B |
| 3 | 3 | C |
| 4 | 2 | D |
+-------+------------+------+
I want to write a query that effects:
"Find all the records from table 1 which are of type 1, take the id's of those records, and find all the records in table 2 where 'table_1_id' which match one of that set of ids."
In the above very oversimplified table example that would result in the query returning records with ids 2 and 4 in table 2
Sounds like your looking for IN:
select *
from table2
where table_1_id in (select id from table1 where type = 1)
Or perhaps you could JOIN the tables:
select t2.*
from table2 t2
join table1 t1 on t2.table_1_id = t1.id
where t1.type = 1
Joining the tables could result in duplicate records. Depends on your needs.
SELECT t1.value,t1.type,t2.value FROM table1 t1,table2 t2 WHERE t1.id = t2.table_1_id AND t1.type = 1;