Echo data from several sql tables - php

I have never tried this but want to echo data from several different tables and order them by their timestamp. The code I have comes up with a booleon and i dont know why.
<?php
$result = mysql_query("SELECT * FROM artists ORDER BY timestamp DESC
UNION
SELECT * FROM news ORDER BY timestamp DESC
UNION
SELECT * FROM tracks ORDER BY timestamp DESC
UNION
SELECT * FROM gigs ORDER BY timestamp DESC
UNION
SELECT * FROM feature ORDER BY timestamp DESC");
while($row = mysql_fetch_array($result)){echo'News: '.$row['name'].$row['title'].$row['trackname'].$row['gigname'].'<br>' ;}
?>

Assuming your tables fall within the constraints of the UNION function..
Try:
SELECT * FROM
(SELECT * FROM artists ORDER BY timestamp DESC
UNION
SELECT * FROM news ORDER BY timestamp DESC
UNION
SELECT * FROM tracks ORDER BY timestamp DESC
UNION
SELECT * FROM gigs ORDER BY timestamp DESC
UNION
SELECT * FROM feature ORDER BY timestamp DESC) as temp
ORDER by timestamp DESC;

Related

MySQL select grouping multiple rows from same table

I'm trying to create a query to select multiple rows from the same table grouping them like an array.
Now i'm selecting them using php like this:
$tks = mysqli_query($con,"SELECT * FROM hof ORDER BY tks DESC LIMIT 5");
$top_ths = mysqli_query($con,"SELECT * FROM hof ORDER BY ths DESC LIMIT 1");
$top_tha = mysqli_query($con,"SELECT * FROM hof ORDER BY tha DESC LIMIT 1");
----
I would like to merge them in a single query so i get an associative array.
Something like this:
(SELECT * FROM hol ORDER BY tks DESC LIMIT 5) AS tks
UNION
(SELECT * FROM hol ORDER BY ths DESC LIMIT 1) AS top_ths
So tks contains all the 5 rows and top_ths contains 1 row.
Is it possible ? Thanks.
to undersatnd from wich group row is, make additional field
(SELECT *, 1 as `group` FROM hol ORDER BY tks DESC LIMIT 5)
UNION
(SELECT *, 2 as `group` FROM hol ORDER BY ths DESC LIMIT 1)

How to get limit records with in Clause

I have a following query
I am getting id as a string
SELECT * from table_name
where id in (1,2,3,4)
order by created desc
limit 0,4
this Query is giving me only 4 records
i want to get 4 records from each id
Any idea ?
select * from
(
SELECT * from table_name where id = 1 order by created desc limit 4
union all
SELECT * from table_name where id = 2 order by created desc limit 4
union all
SELECT * from table_name where id = 3 order by created desc limit 4
union all
SELECT * from table_name where id = 4 order by created desc limit 4
) tmp
order by created desc

MySQL Query - Show Latest 3 Records with Order by Ascending

Is there anyway to fetch latest 3 comments with Order By id asc ?
Here is my table Structure: Table name: comments
Right Now I am using this Query:
SELECT *
FROM `comments`
ORDER BY id ASC
LIMIT 0 , 3
But it returns in result, which is obvious :
But I want to show latest 3 records , but in Ascending Order.
Like this:
Use below code:
SELECT *
FROM (SELECT *
FROM `comments` ORDER BY id DESC LIMIT 0 , 3) t
ORDER BY id ASC;
First you sort by descending id, and get 3 results, and then do ascending sort on id on these 3 results.
(SELECT * FROM `comments` ORDER BY id DESC limit 3 ) ORDER BY id ASC
Just reorder the DESC query with a second ORDER BY :)
SELECT * FROM (
SELECT *
FROM comments
ORDER BY id DESC
LIMIT 3
) t ORDER by id ASC
try this
select * from (select * from `comments` ORDER BY id desc limit 0,3) t
order by id asc;
This should do it:
SELECT *
FROM `comments`
ORDER BY id DESC
LIMIT 0 , 3

Mysql Query: switch if record come from table news or news_collect

I have this query:
(SELECT * FROM news WHERE Del='0' ORDER BY `Date` DESC)
UNION
(SELECT * FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)
Now in the while loop I need to switch if record come from table news or news_collect...How do it??
I would solve this problem adding an custom field, keeping the column count the same.
(SELECT *, 'news' as source FROM news WHERE Del='0' ORDER BY `Date` DESC)
UNION
(SELECT *, 'news_collect' as source FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)
Then you can use switch($aRow['source']).
Try modifying your query as follows:
(SELECT news.*, 'news' coming_from FROM news WHERE Del='0' ORDER BY `Date` DESC)
UNION
(SELECT news_collect.*, 'new_collect' FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)
(assuming "coming_from" is not a column of the "news" table)
Then use column "coming_from" to distinguish between the record's origin.

Running a query with PHP/MySQL then reordering the results by another column

This SQL query gives me the results I want; however, I want the results ordered by a different column:
SELECT *
FROM post
INNER JOIN account ON post.account_id = account.account_id
WHERE post_id > new
ORDER BY post_date
ASC LIMIT 10;
I can not simply change ORDER BY post_date ASC to ORDER BY post_id DESC, while that will in fact order the query the way I want it... it will give me the wrong 10 posts.
I simply want to take the EXACT RESULTS of the above query then reorder the results by the post_id.
I would like to do this with SQL if possible, if not I could order the results by adding the results into a new array reversed.
Use a subquery to reorder:
SELECT * FROM (
SELECT *
FROM post
INNER JOIN account ON post.account_id = account.account_id
WHERE post_id > neww
ORDER BY post_date ASC LIMIT 10;
) ORDER BY post_id
Use a subquery:
SELECT * FROM (
SELECT *
FROM post
INNER JOIN account
ON post.account_id = account.account_id
WHERE post_id > neww
ORDER BY post_date ASC
LIMIT 10) AS T1
ORDER BY post_id DESC

Categories