MySQL Multiple Table Query and Results - php

I am trying to query multiple tables and trying to display the results as follows...
Tables...
news
id title news_datestamp
1 news 1 01/01/2001
2 news 2 01/05/2001
articles title articles_datstamp
1 article 1 01/04/2001
2 article 2 01/06/2001
I'm trying to get a single script to display the results as...
News 1 01/01/2001
Article 1 01/04/2001
News 2 01/05/2001
Article 2 01/06/2001
Any ideas?

You could simple use a Union to merge results in a subselect and sort the final table:
SELECT * FROM (
SELECT id, title, news_datestamp AS datestamp, "news" AS type FROM news
UNION ALL
SELECT articles AS id, title, article_datestamp AS datestamp, "article" AS type FROM articles
) AS temptable
ORDER BY datestamp
The type column will help you to identify id references later.

It looks to me like you want a union of the news and article tables.
SELECT * FROM
( SELECT title,
news_datestamp AS datestamp
FROM news
UNION ALL
SELECT title,
article_datestamp AS datestamp
FROM articles
) AS everything
ORDER BY datestamp, title

Related

my Sql query to select 2 records per each category

I have 2 tables. 1. news table 2. category table.
in news table i have id, title, category id
in category table i have id and category name.
in news table there are many news items where multiple news items for each category.
what i need was to select 2 record per each category from news table, also i need to join those two tables, so that i can get the name of the category from category table.
i tried using below query
SELECT * FROM (
SELECT
news.id, news.fk_lookups_category, lookups_category.name, news.title, news.description,news.datetime,lookups_category.priority
FROM news
JOIN lookups_category ON news.fk_lookups_category=lookups_category.id
WHERE
news.isPublished='1' and news.datetime >= ('today' - INTERVAL 7 day) order by datetime DESC
) as newitem order by priority

Can I do this with 1 query?

I am creating a simple blog with categories in php.
I want that myblog.com/category.php?id=3 show me this:
TITLE of the category 3
// other stuff
ALL POSTS of the category 3
So, actually I do 2 queries ( 1 for getting the title and 1 for getting the posts ).
Is there a way to do this with 1 query ?
Depending on your database tables, you could something like that
SELECT c.title, p.data FROM category c LEFT JOIN post p ON p.category = c.category ORDER BY p.date
The category title will be repeated for every post though
If you need all fields from these tables so I think you should use #Damien's way.
If you need only titles you can use following query. In this query the first row is a title of a CATEGORY and next rows are titles of posts.
select * from
(
select 0 as ordField, categoris.Category_TITLE as Title from categoris where id =3
union all
select 1 as ordField, POSTS.Post_TITLE as Title from POSTS where category_id=3
) t order by ordField

How to find required unique result

I have two table 'topic' and 'subcategory'
I am using this query--
Select * from `subcategory` as s
Inner join `topic` as f
WHERE s.`Subcategory_id` = f.`Subcategory_id
My result shows like
Category_id Subcategory_id Post_id time
2 2.3 4 2012-12-01
1 1.5 5 2013-01-20
1 1.3 6 2013-03-18
There's also other columns... but all I want is to select the latest Post_id and Subcategory_id of one Category_id ... that means here Category 1 has two Subcategory it will select only the latest(here 1.3) and same result all the time for all Category when database will grown larger. What will be the next query or how could I change the existing query to gain my desired result?
SELECT Post_Id, Subcategory_Id from subcategory as s, topic as t where
s.Subcategory_id = t.Subcategory_id and time = (
SELECT Max(time) from subcategory as s1, topic as t1 where
s1.Subcategory_id = t1.Subcategory_id and s1.Category_id = s.Category_id
);
Something like that, I think, will work.
SELECT TOP 1 ... ORDER BY whatever column determines "the latest"
e.g.
SELECT TOP 1 ... ORDER BY TIME DESCENDING
Or in case of mysql:
SELECT ... ORDER BY TIME DESCENDING LIMIT 1
Join your topic table with following query:
SELECT s.* FROM subcategory s
Inner JOIN (SELECT s1.Category_id,
MAX(s1.time1) AS max_time
FROM subcategory s1
GROUP BY s1.Category_id) y
ON y.Category_id = s.Category_id AND y.max_time = s.time1

SQL Table Structure for news site

I want to make a fast table structure for news site with php and mysql. My database structure is ID, title, content, cat_ids (; separedet IDs of categories - ;5;10;15;20;), active, publish_date.
I want to make a fast query to select news from this table. Something like that:
SELECT id
FROM news
WHERE cat_ids LIKE '%;15;%'
AND active = 1
AND publish_date < NOW()
ORDER by publish_date DESC
LIMIT 0, 10
But if my table is 2-3GB the query is very slow. I need some ideas to make structure and make the select faster.
Instead of using cat_ids column, try creating a news_cats table with news_id and cat_id, and using this query:
SELECT id
FROM news JOIN news_cats ON news_id = id
WHERE cat_id = 15
AND active = 1
AND publish_date < NOW()
ORDER by publish_date DESC
LIMIT 0, 10
Some suggestions as below:
1) Create index on "active" field
2) Create index on "publish_date" field
3) Create separate table for category and news relation and remove "cat_ids" field from news table
New table might look like below:
news_category_ids
news_id
category_id
It can have multiple rows for each news_id, if news item falls in 3 categories, it will have 3 rows
Then use SQL like below:
SELECT news.id
FROM news INNER JOIN news_category_ids ON news.id = news_category_ids.news_id
WHERE 1
AND news.active = 1
AND news_category_ids.cat_id = 15
AND news.publish_date < NOW()
ORDER by news.publish_date DESC
LIMIT 0, 10

Group by ID and Show all results from mysql

I have 2 tables
tabcats - Cat_Id, Cat_Name
tabnews - News_Id, News_Name, Cat_Id
So I'm trying to make a select on database and return all results but using
Group by Cat_Id
so my results was supposed to be
let's say I have 3 Categories and 5 News
Results
Cat_Name 1
News_Name (1)
News_Name (2)
Cat_Name 2
News_Name (3)
News_Name (4)
Cat_Name 3
News_Name (5)
I read something about using LEFT OUTER JOIN but I don't get this clearly.
First you need to be clear about yourself. What I supposed that you tried to do is select some counts of latest news,isn't it? Try query similar to this one: The below query Find the course ID, semester, year and title of each course offered by the Comp. Sci. department
select section.course_id, semester, year, title from section, course where section.course_id = course.course_id and dept_name = ‘Comp. Sci.'
Ordering can be done as follows:
return(mysql_query("SELECT * FROM tabNews GROUP BY news_id DESC LIMIT *counts*"));
This is to list all news together with cat name sorting by cat name follow by news name.
SELECT News_Name,Cat_name from tabNews n left join tabCat c on n.cat_id=c.cat_id
ORDER BY c.cat_name,n.news_name

Categories