Merging 2 result sets of colums of an mysql query into one - mysql

Example:
First Query:
select A,B,C from tb1;
---------------
A B C
---------------
1 1 3
2 1 4
Second Query:
select E from tb2;
---------------
E
---------------
8
9
The required result format should be like this:-
-----------------------
A B C E
-----------------------
1 1 3 8
2 1 4 9
Please tell me the query how to get the result set.

to make your tables better structured if you can add in both tables a column id to identify the row number of each table.
then you can easily merge two table which have same id . the relation here is the id column.
then you could do this
select A, B, C , E from Table1 t1
inner join Table2 t2
on t1.id = t2.id
please see structured tables in this DEMO HERE
THE RESULT:
A B C E
1 1 3 8
2 1 4 9
hope it helps you !

SQL tables are inherently unordered. This poses a problem, because there is nothing in your table to specify the ordering of the rows, which seems to be the connection between the two tables.
So, you have to hold your breath and make some assumptions. The particular assumption is that the data returned by a select is the actual order you want in the table (it could be different if you have deletes in the table or are running in a parallel environment).
The following code adds an id to each table. This id can then be used for the join:
select t1.A, t1.B, t1.C, t2.D
from (select t1.*, #rn := #rn + 1 as id
from tbl1 t1 cross join (select #rn := 0)
) t1 left outer join
(select t2.*, #rn := #rn + 1 as id
from tbl2 t2 cross join (select #rn := 0)
) t2
on t1.id = t2.id

Hi may be it's help for you
you can try like this
SELECT * FROM tb1,tb2
And you get output like
-----------------------
A B C E
-----------------------
1 1 3 8
2 1 4 9

Related

SQL query to join two tables with no repeated values?

Table 1
ID | NAME | WARD_ID|
1 A 1
2 B 1
3 C 2
4 D 2
Table 2
ID | MONTH1 | MONTH2 | WARD_ID|
1 9 10 1
2 6 11 1
3 5 12 2
4 13 14 2
I want to join this two table and produce the following output:
ID | NAME | MONTH1 | MONTH2 | WARD_ID|
1 A 9 10 1
2 B 6 11 1
3 C 5 12 2
4 D 13 14 2
In the ON condition of the query I have to keep WARD_ID equal for both the tables. I could not able to figure out the solution. Anyone have any experience with a query like this?
I think you want something like this:
select t1.*, t2.*
from (select t1.*,
(#rn1 := if(#w1 = ward_id, #rn1 + 1,
if#w1 := ward_id, 1, 1)
)
) as rn
from (select t1.* from table1 t1 order by ward_id, id ) t1 cross join
(select #w1 := -1, #rn1 := -1) params
) t1 join
(select t2.*,
(#rn2 := if(#w2 = ward_id, #rn2 + 1,
if#w2 := ward_id, 1, 1)
)
) as rn
from (select t2.* from table2 t2 order by ward_id, id ) t2 cross join
(select #w2 := -1, #rn1 := -1) params
) t1
on t2.ward_id = t1.ward_id and t2.rn = t1.rn;
The subqueries enumerate the rows in each table. The join then uses the enumeration.
This is much simpler in MySQL 8.0, using row_number().
I'm assuming here that ID is intended to be the same from both tables. If so, I think you can do a multi-condition join:
select * from table1 t1
inner join table2 t2
on t1.ID=t2.ID and t1.WARD_ID=t2.WARD_ID
You can do something like:
SET #rn:=0;
SET #rn2:=0;
SELECT *
FROM (
SELECT #rn:=#rn+1 AS rn1, t1.ID, t1.NAME, t1.WARD_ID
FROM t1
GROUP BY t1.WARD_ID, t1.NAME
ORDER BY t1.WARD_ID, t1.NAME
) s1
INNER JOIN (
SELECT #rn2:=#rn2+1 AS rn2, t2.ID, t2.MONTH1, t2.MONTH2, t2.WARD_ID
FROM t2
GROUP BY t2.WARD_ID, t2.MONTH1,t2.MONTH2
ORDER BY t2.WARD_ID, t2.MONTH1,t2.MONTH2
) s2 ON s1.WARD_ID = s2.WARD_ID
AND s1.rn1 = s2.rn2
But it really doesn't reliably sort the tables to join the same rows every time. I still think there isn't a reliable/repeatable way to join the two tables the same every time.
============================================================
http://sqlfiddle.com/#!9/aa2db0/1 <<<< If ID can be used to reliably sort the two tables, you can use it in the ORDER BYs. I've added it in this Fiddle, and included rows in the setup that would fall before the existing records and potentially change the sorting. This also includes more records in Table 2 than there are in Table 1, so would possibly result in duplicated rows. These new rows are ignored since they can't be matched between tables.

insert to from multiple to one table in mysql

I want to select from two table column datas and insert it into another table I can this but it copy 6 times.
I tried this code it works but insert 6 time in to table3.
insert into table3(LisanNo , UserName, table1ID,
NameAndLastName , table2Id)
select table1.LisansNo, table1.UserName, table1.Id,
table2.NameAndLastName,table2.Id
form table1, tabl2;
Enter code here-
**Table1**
id LisansNo UserName
1. f3ewrwer aaa
2. r3we3 bbb
**Table2**
id NameLastName
3. john ddd
4. hhhhh
9. yyyy
11. terere
I want to insert this Two table to another table.
table 3
id LisanNo UserName table1ID NameLastName Table2ID
1. f3ewrwer aaa 1 john ddd 3
2. r3we3 bbb 2 hhhhh 4
3. yyyy 9
4. terere 11
You seem to want to list the values vertically. This is not a very SQL'ish thing to do, but you can accomplish it:
select t1.LisansNo, t1.UserName, t1.Id,
t2.NameAndLastName, t2.Id
from (select t1.*, (#rn1 := #rn1 + 1) as seqnum
from table1 cross join
(select #rn1 := 0) params
) t1 left join
(select t2.*, (#rn2 := #rn2 + 1) as seqnum
from tabl2 t2 cross join
(select #rn2 := 0) params
) t2
on t1.seqnum = t2.seqnum;

Mysql update where each updated row has an increasing number

Is it possible to run a MySql query that updates certain rows, but gives the updated value in each row an increasing value.
I looked into stored procedures for this but I was not able to create such a procedure.
Here is a sample to help you better understand what i want to achieve:
The table
id text myCustomValue myMatchValue
1 Test1 1 a
2 Test2 2 a
3 Test3 3 a
4 Test4 1 b
5 Test5 1 c
6 Test6 2 b
7 Test7 3 b
My custom query should basically achieve this:
UPDATE table SET myCustomValue = increasingNumberStartingAt_5 WHERE myMatchValue = a;
With this query the result would be:
id text myCustomValue myMatchValue
1 Test1 5 a
2 Test2 6 a
3 Test3 7 a
4 Test4 1 b
5 Test5 1 c
6 Test6 2 b
7 Test7 3 b
I need this because the table is large and thus I cannot run many queries to the database to update each row individually. I could send multiple statements at once, but it seems like there should be a much simpler solution that i just cannot think of.
You can do something like this
UPDATE mytable e,
(SELECT #n := 4) m
SET e.myCustomValue = #n := #n + 1
WHERE e.myMatchValue ='a'
SQL-Fiddle
Try this:
UPDATE mytable AS t1
INNER JOIN (
SELECT id, #rn := #rn + 1 AS rn
FROM mytable
CROSS JOIN (SELECT #rn := 4) AS var
WHERE myMatchValue = 'a'
ORDER BY id
) AS t2 ON t1.id = t2.id
SET t1.myCustomValue = t2.rn
The above UPDATE statement uses an INNER JOIN to a derived table that uses a variable in order to enumerate matching records of the table. The first matching record is assigned a value of 5, next record a value of 6, etc.
Demo here
UPDATE YOUR_TABLE
INNER JOIN (
SELECT
T1.ID,
#a := #a + 1 AS incr
FROM
(SELECT #a := 4) sl,
YOUR_TABLE T1
INNER JOIN YOUR_TABLE T2 ON T1.ID = T2.ID
WHERE
T1.myMatchValue = 'a'
) filteredTable ON YOUR_TABLE.ID = filteredTable.ID
SET YOUR_TABLE.myCustomValue = filteredTable.incr;
RUNNING SQL FIDDLE
You could try this:
SET #start_at = 4; -- 1 minus your desired start point
UPDATE
my_table a
INNER JOIN (
SELECT
id, #start_at := #start_at + 1 AS _row
FROM
my_table
WHERE
myMatchValue = 'a' ) b
ON
a.id = b.id
SET
a.myCustomValue = b._row;
Here: sqlfiddle

Left outer join to two tables

I have a table 1 with a one to many relationship to table 2.
Table 1 also has a one to many relationship with table 3
I want to combine the results of the join but all im getting is repeated values
Here is the structure:
table 1
reportnumber
1
2
3
table 2
reportnumber col1
1 a
1 b
2 c
3 a
table 3
reportnumber col2
1 x
1 y
1 z
2 w
expected result set
reportnumber col1 col2
1 a x
1 b y
1 z
2 c w
3 a
I'm sure this is possible with a left outer join but i just cant get the syntax right
Any clues?
This is what im trying
select * from table1 a
left outer join table2 b on a.reportnumber=b.reportnumber
left outer join table3 on a.reportnumer=c.reportnumber
But the results look like this
reportnumber col1 col2
1 a x
1 a y
1 a z
1 b x
1 b y
1 b z
...
This isn't easy in MySQL, but you can do it with variables. This has little to do with a join. Or, it has a lot to do with join, but you don't have the right join keys and you don't have full outer join.
The solution is to enumerate the rows from each table with the data columns. Then aggregate using the enumeration and reportnumber:
select reportnumber, max(col1) as col1, max(col2) as col2
from ((select t2.reportnumber, col1, null as col2, #rn2 := #rn2 + 1 as rn
from table2 t2 cross join
(select #rn2 := 0) const
) union all
(select t3.reportnumber, null, t3.col2, #rn3 := #rn3 + 1 as rn
from table3 t3 cross join
(select #rn3 := 0) const
)
) t
group by reportnumber, rn;

MySQL: Selecting all the rows which are related with a particular value from another row

I have the following mysql table. I have been trying to select all the rows which are related with the row that has B as value in the code column. The relation is based on the value of the column trans_id
id trans_id code amount side
1 1 A 200 left
2 1 B 200 right
3 2 J 100 right
4 2 C 100 right
5 2 B 200 left
6 3 A 630 right
7 3 K 630 left
My Expected Result:
id trans_id code amount side
1 1 A 200 left
3 2 J 100 right
4 2 C 100 right
Could you please tell me what should be the mysql query to achieve this?
Thanks :)
The following query should return the results you want. This uses a select query to return results to the WHERE clause.
SELECT * FROM yourTable
WHERE trans_id IN (
SELECT trans_id FROM yourTable WHERE code='B'
)
AND code!='B'
Your question is unclear, but as far as I understand, you could use a self join like this:
select a.id,
a.trans_id,
a.code,
a.amount,
a.side
from table as a
inner join table as b on (a.id=b.trans_id and b.code='B');
This will return the row with table.id=2:
id trans_id code amount side
2 1 B 200 right
select
t1.*
from
table_name t1
inner join table_name t2 on t1.trans_id = t2.trans_id
where
t2.code = 'B' and
t2.code <> t1.code
If I'm understanding your problem correctly then a subquery would get you what you need.
SELECT * FROM yourTable WHERE id IN (SELECT trans_id FROM yourTable WHERE code='B')