Forum Moderators: coopster

Message Too Old, No Replies

How to Link Result Table of 2 Queries?

possible?

         

AthlonInside

4:33 pm on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I have 2 table, I can easily linked them with where something = something.

But I have 2 tables that I generated from complicated queries, and now I want to link them together too with where something = something.

How will the MYSQL query look like?

AthlonInside

4:51 pm on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's make things clear

If I have 2 query (simplified)

select * from tableA where "a lot of conditions";

select * from tableB where "a lot of conditions";

both query will create a different result table. Is it possible in some way to JOIN this 2 table and make them in one query? I mean how to I make the first quest as "Table 1" and query 2 as "Table 2" so I can do something like

select * from "Table 1", "Table 2" where "more conditions".

paladin

5:38 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



That sound like you need a join/inner join. If so you can find some more info at [dev.mysql.com...]

panos

5:44 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



Depends on the conditions.
Do the 2 tables have something in common?

I will try to make an example
Lets say we have table1:

id
country
population
flag

and table2

id
city
country_id

We want to select a flag to put account a city name:

SELECT flag FROM table1,table2 WHERE table1.id=table2.country_id

Hope this helped :)

AthlonInside

6:20 pm on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is definitely a JOIN. But my problem is I am not joing 2 existing tables. I am trying to JOIN 2 tables that is a result of 2 complicated queries.

For example,

select fieldA, fieldB, count(*) from tableA where something1 and something2 groupB, fieldA, fieldB order by fieldA desc

So this complicated query return a table. How do I, in any way, use the result of the QUERY to feed as a table to another query?

Salsa

6:51 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



Maybe you are looking for subselects?:

SELECT stuff FROM table1 WHERE something = (SELECT something_else FROM table2) [AND/OR ...];

...only available since MySQL 4.1.

panos

11:14 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



You could also use temporary tables or you could just save the elements of the first query in an array and then use php to do the job.

AthlonInside

5:42 am on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you for your help.

I think I am looking for subselect. But most web host still doesn't run MySQL 4.1 :(

I used to do everything with PHP, but later I found out MySQL is so powerful in doing all those sorting and calculations itself! So I am trying to master them now.

What I love most is the aggregate functions of group by!

count(DISTINC field)
sum()
max()

They are just too powerful and save a lot of time processing with PHP. Why remake the wheel? haha.

I also read that is a new ROLLUP in MySQL 4.11 where it will add the TOTAL to the end of the result. COOL!