tags:

views:

101

answers:

2

If you go to http://www.foxnews.com, and scroll down a little you would see the box "LATEST NEWS".

Now I want to do something like that, but with modification.

I have 3 queries, that get out information from 3 tables. But this doesn't give me the "latest news", because it's 3 separate queries.

Now I want to make a box, (I can do this in css), that displays the latest column of those 3 tables, sorted by dato DESC.

These are the tables:

Tablename: Member_meetups

Column to show: meetup

Table: member_film

Column to show: navn

Table: member_tutorials

Column to show: title

Sort by dato.. All these tables have the column "dato" too, so I want to sort out from dato DESC..

And in that way that would be latest actions from those 3 tables..

FOR EXAMPLE it would show like this: Latest Actions: I love Meetups - 23:11:02 - 01/01/01 (a column in member_meetups) I love tutorials - 23:10:00 - 01/01/01 (a column in member_tutorials) I love tutorials first - 22:59:00 01/01/01 (a column in member_tutorials) I hate movies! - 22:42:00 01/01/01 (a column in member_film)

and so on.. as you see sorting by DATO, shows the last column's title+dato in the 3 tables.

Questions please comment, and please answer with not so hard English, as you may hear, I'm not English myself. Thank you

My question is: How to do this?

A: 

You don't actually ask a question from what I can gather.

But I shall try to answer your question as best as I can.


You should use mySQL join. (I'm assuming you are using mySQL).

You can then do what you want in a single query. If you provide more information on your DB schema then it will be possible to construct the exact query for you.

Laykes
Sorry i did never actually write a question. But i am using me of mysql and php, and im new with this, so what/where can i get the Database schema?
Karem
Just explain the tables more. Edit your question above.
Laykes
Ok please check the updated question
Karem
+1  A: 

You want to use UNION. Here's a sample query:

SELECT meetup AS text, dato FROM Member_meetups UNION
SELECT navn AS text, dato FROM member_film UNION
SELECT title AS text, dato FROM member_tutorials
ORDER BY dato DESC
LIMIT 10;

I only selected the columns you indicated; if you're going to need more columns from each table then alter the query as you need to. Just make sure you line up the column names using the AS function as needed to normalize the data.

You should think about caching this data; this can often be quite a slow query.

Edit to answer question in comments:

It's not entirely clear to me how you want it to work, and it's difficult to write a full query without your DB's schema, but here's the gist:

SELECT m.meetup AS text, m.dato FROM Member_meetups m
INNER JOIN member_battles b
ON m.battle=b.id
WHERE b.fighter1=$pUsername OR b.fighter2=$pUsername UNION
SELECT navn AS text, dato FROM member_film UNION
SELECT title AS text, dato FROM member_tutorials
ORDER BY dato DESC
LIMIT 10;

Again, that's very rough because I don't know your DB's schema or quite understand what you're wanting the query to do. If you need more specific help, I'd suggest either editing your question or starting a new one. It's very difficult to work through these types of things with comments.

Brock Batsell
but how should i echo the query then? you know <? echo...?
Karem
The results will be a table with two columns, text and dato, with the contents of all 3 tables combined, and you can loop through it just like if it was returning a single table.If you're using MySQL procedurally:while ($row = mysql_fetch_assoc($query)) { echo $row['text'] . ' - ' . $row['dato'];}
Brock Batsell
Hello thank you this works perfectly!! So if i need more columns i should do e.g : SELECT meetup AS text, battle AS text, dato FROM member_meetups ?
Karem
I don't think you'll be able to rename two columns from the same table as "text". I'm not sure what the "battle" column holds, but instead of "AS text", use "AS <a relevant name>", then if you have a similar column in your other tables, use the same "AS <a relevant name>" statement to match them up and you'll end up with a results table with 3 columns: text, <a relevant name>, and dato.
Brock Batsell
Hello. I just to know this because i want to SELECT either "fighter2" or "fighter1" which contains = $pUsername in member_battles. Can i do this in this query or would it be too difficult.. Hope im not asking too much
Karem
Edited to attempt to answer your question, though it's a little unclear to me.
Brock Batsell