how to get two tables data based on column - json

We have two tables Table1 and table2. Table1 have A,B,C columns and Table2 have B,E,F columns.
Table1 Table2
A B C B E F
raja 21 HYD 21 AP 60000000
raju 21 VJY
ravi 21 BEG
We need is
A B C E F
raja 21 HYD AP 60000000
raju 21 VJY AP 60000000
ravi 21 BEG AP 60000000
so We tried like this
tx.executeSql('SELECT r.A as name,r.B as age,r.C as city,s.E as state,s.F as distance from table1 r Left JOIN table2 s on r.B=s.B',[],joinsuccess,errorCB);
function joinsuccess(tx,result){
var JOINLength = result.rows.length;
for (var i=0; i<JOINLength; i++){
console.log(result.rows.item(i).name);
console.log(result.rows.item(i).age);
console.log(result.rows.item(i).city);
console.log(result.rows.item(i).state);
console.log(result.rows.item(i).distance);
}
}
but it's we got only
ravi 21 BEG AP 60000000

Related

mysql select max group by id

good evening,
i have a table:
A B C
45 1 1
22 2 1
40 3 1
43 1 2
21 2 2
61 3 2
49 4 2
60 5 2
76 1 3
41 2 3
57 3 3
i find max(A) from max(B) group by C. The result should be 60 - max number in A from last row in B from each group (C)
Thank you for your help
If i understand correctly your question you could use an inner join on select max(b):
select max(A)
from my_table m
inner join (
select C,
max(B) act_B
from my_table
group by C
) t on t.act_B = m.B and t.c = m.c

MySQL - Select distinct detecting changes on different ordered rows

I have a table like this:
T A B C ID
2015-07-19 a b c 1
2015-07-16 a y z 2
2015-07-21 a b c 1
2015-07-17 a y c 2
2015-07-18 a y c 1
2015-07-20 a b c 1
2015-07-17 a y c 1
2015-07-19 a b c 2
2015-07-16 a y z 1
2015-07-20 a b c 2
2015-07-15 a y z 1
2015-07-22 x b c 1
2015-07-21 a b c 2
2015-07-18 a y c 2
2015-07-15 a y z 2
2015-07-22 a y c 2
2015-07-14 x b c 1
I need to get an ordered result by datetime column T, but I need that the query detects and avoid repeated rows in columns A, B and C. And all this ordered and separated by ID.
It could be a stored procedure. It's important to be fast, because is a huge log table. With millions of rows.
The result should be like this:
T A B C ID
2015-07-22 x b c 1
2015-07-19 a b c 1
2015-07-17 a y c 1
2015-07-15 a y z 1
2015-07-14 x b c 1
2015-07-22 a y c 2
2015-07-19 a b c 2
2015-07-17 a y c 2
2015-07-15 a y z 2
Any ideas?
This query gives the expected result (tested):
SELECT t1.* FROM mytable t1
LEFT JOIN mytable t2 ON t1.t = t2.t + INTERVAL 1 DAY AND t1.A = t2.A AND t1.B = t2.B AND t1.C = t2.C AND t1.ID = t2.ID
WHERE t2.T IS NULL
ORDER BY t1.ID, t1.T DESC
Try this query:
SELECT max(t),a,b,c,id FROM table GROUP BY A,B,C,id ORDER BY ID, max(T)
Try this query:
SELECT * FROM table GROUP BY A,B,C,T ORDER BY ID, T
Edit: added T to group by

Unioning and Joining in 1 Query?

I have the following tables:
TABLE1:
A B C
1 2 3
2 4 6
3 6 9
TABLE2:
A B C
4 8 12
5 10 15
6 12 18
TABLE3:
A D
2 X
4 Y
6 Z
I need one query that gives:
A B C D
1 2 3
2 4 6 X
3 6 9
4 8 12 Y
5 10 15
6 12 18 Z
Is that possible?
I can do it in 2 queries, but the person I'm doing it for wants it in 1.
Thanks!
Try this (example on sqlfiddle):
SELECT x.a, x.b, x.c, d
FROM (
SELECT a, b, c FROM table1
UNION ALL
SELECT a, b, c FROM table2
) x
LEFT JOIN table3 ON ( table3.a = x.a )
Sure:
select v1.*, table3.d
from
(select table1.a, table1.b, table1.c
from table1
union all
select table2.a, table2.b, table2.c
from table2
) v1
left join table3 on v1.a = table3.a

Using mySQL to compare 2 tables

I'm trying to compare 2 mySQL tables to find differences between them. A record may be found in TableA but not in TableB, or vice versa.
My tables are as follows:
TableA
Name A1 A2 B1 B2
------------------------
John 11 12 21 23
John 11 12 21 22
John 33 34 31 33
Mary 41 42 54 55
Mary 71 72 81 82
Mary 41 42 51 52
TableB
Name A1 A2 B1 B2 C D
---------------------------------
John 11 12 21 22 999 999
John 21 23 11 12 999 999
John 31 32 33 34 999 999
Mary 41 42 51 52 999 999
Mary 54 55 41 42 999 999
Columns A1 and A2 is considered a group, and B1 and B2 considered another group. For a record to be considered found in both tables, I need
- TableA(A1,A2) = TableB(A1,A2) AND TableA(B1,B2) = TableB(B1,B2)
OR
- TableA(A1,A2) = TableB(B1,B2) AND TableA(B1,B2) = TableB(A1,A2)
For the 2 tables, above, I would compare all of TableA's John to all of TableB's John, and all of TableA's Mary to all of TableB's Mary.
I should get the output
Name A1 A2 B1 B2 C D
-----------------------------------------------
John 31 32 33 34 999 999 (from TableB)
Mary 41 42 54 55 (from TableA)
Mary 71 72 81 82 (from TableA)
Mary 54 55 41 42 999 999 (from TableB)
I'm new to mySQL, and the above seems so complicated to me that I'm not even sure where to start.
I would really appreciate any help on this.
If I understood you correctly, you need to issue two queries: one for finding records from TableA not existing in TableB, and second one for the opposite situation. Note that in one case it's LEFT JOIN and in the second case it's RIGHT JOIN.
SELECT a.*, '' AS C, '' AS D, '(from TableA)' AS 'table'
FROM TableA AS a
LEFT JOIN TableB AS b
ON ((a.A1 = b.A1 AND a.A2 = b.A2 AND a.B1 = b.B1 AND a.B2 = b.B2)
OR (a.A1 = b.B1 AND a.A2 = b.B2 AND a.B1 = b.A1 AND a.B2 = b.A2))
AND a.Name = b.Name
WHERE b.Name IS NULL
UNION
SELECT b.*, '(from TableB)' AS 'table'
FROM TableA AS a
RIGHT JOIN TableB AS b
ON ((a.A1 = b.A1 AND a.A2 = b.A2 AND a.B1 = b.B1 AND a.B2 = b.B2)
OR (a.A1 = b.B1 AND a.A2 = b.B2 AND a.B1 = b.A1 AND a.B2 = b.A2))
AND a.Name = b.Name
WHERE a.Name IS NULL

Select FROM 3 Table

Well I think it's a simple question but I could not found the solution.
Well I have three Tables:
Table 1
id(AS t1id) Name LASTNAME Value
1 a z 50
2 b e 60
3 c k 70
4 d u 60
Table2
id idTable1 Name(AS t2me) Value(AS t2ve)
1 1 er 50
2 1 zx 150
3 2 zc 300
Table 3
id idTable1 Name(AS t3me) Value(AS t3ve)
1 2 erxc 50
2 2 zvvx 150
3 2 zcz 300
How to get this result with SQL
t1id Name LASTNAME t2me t2ve t3me t3ve
1 a z er 50 erdxc 150
1 a z zx 150
2 b e zc 300 erxc 50
2 b e zvvx 150
2 b e zcz 300
Is that possible? If not what could I do?
SELECT t1.id as t1id, t1.Name, t1.LASTNAME,
t2.Name as t2me, t2.Value as t2ve, t3.Name as t3me,
t3.Value as t3ve from Table1 t1
LEFT JOIN Table2 t2 on t1.id = t2.idTable1
LEFT JOIN Table3 on t3.idTable1 = t1.id