Syahmi Roslan
Syahmi Roslan

Reputation: 110

How to combine two tables in SQL (MySQL)?

I want to combine between two table. The output should be data from table A, table B.

 SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success 
 FROM transfer_tx_201503 WHERE time >='00:00:00' AND time <= '$searchterm' 
 UNION SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success FROM request_tx_201503 WHERE time >='00:00:00' AND time <= '$searchterm' GROUP BY date desc"

i want out put like this: |2015-03-23 | 5 | 3 | 4 | 1 |

5 and 3 from table transfer_tx_2015, 4 and 1 from table request_tx_2015

Thank you

Upvotes: 1

Views: 196

Answers (4)

XPscode
XPscode

Reputation: 97

SELECT coloumn FROM Table_One, Table_Two WHERE Table_One.coloumnName = Table_One.coloumnName

Upvotes: 1

Aown Raza
Aown Raza

Reputation: 2023

SELECT columns 
FROM firstTable 
JOIN secondTable ON 
    firstTable.columnName = secondTable.columnName

Upvotes: 4

sayani
sayani

Reputation: 148

The common field is the date field, hence the join should be on that field. Try using the following SQL:

SELECT t.date as Date, COUNT(*) as Transaction, SUM(t.status=0) as Success, COUNT(*) as Request, SUM(r.status=0) as RequestSuccess 
FROM transfer_tx_201503 AS t,request_tx_201503 AS r WHERE t.time >='00:00:00' AND t.time <= '$searchterm' AND t.date=r.date

Upvotes: 1

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

You can use table_a, table_b syntax:

SELECT columns FROM table_a, table_b WHERE table_a.id = table_b.id

Or you can use JOIN:

SELECT columns FROM table_a JOIN table_b ON table_a.id = table_b.id

You can read more about JOIN in:

https://dev.mysql.com/doc/refman/5.5/en/join.html

Upvotes: 6

Related Questions