D_R
D_R

Reputation: 4962

Improving SQL query so that you could get information from other tables using current table fields

How can i improve this query so i could insert also data from 'refer' table using 'refer_id' that i'm getting from the facts table..

this is my query

REPLACE INTO `table_name`
        SELECT network_id, type_id, topic_id, COUNT(*), date_id FROM `facts` WHERE `action_id`='1000' AND `type_id` != 17 GROUP BY topic_id, date_id

And the facts i'm reciving is (For example)

Network ID | Type ID | Topic ID | Count | Date

 1     |   2     |   983    |  113  | 03/01/2012
 1     |   3     |   172    |  93   | 03/01/2012

I am trying also to get the refer name(refer table) by the refer id(facts table) from the facts table.. how can i do that?

Upvotes: 0

Views: 53

Answers (1)

Eric
Eric

Reputation: 95133

You can do an inner join to get from one table to the other:

select
    *
from
    facts f
    inner join refer r on
        f.refer_id = r.refer_id

To insert data into a table from another table:

insert into refer (refer_id, refer_name)
select
    refer_id,
    name
from
    facts

Upvotes: 1

Related Questions