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
Related
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
I'm having a hard time to get to the right query/queries here. When you want to find related items using tags, in MySQL, you can use a 'common tag count' to find items that are most similar.
Say my schema looks like this:
tags(tag_id, title)
articles(article_id, some_text)
articles_tags(tag_id, article_id)
Then you can get items and sort them on common tags with 'article 2' for example, like this:
SELECT at1.article_id, Count(at1.tag_id) AS common_tag_count
FROM articles_tags AS at1 INNER JOIN articles_tags AS at2 ON at1.tag_id = at2.tag_id
WHERE at2.article_id = 2
GROUP BY at1.article_id
HAVING at1.article_id != 2
ORDER BY common_tag_count DESC;
But in my situation, there's a challenge. I want to find similar articles based on multiple articles instead of one (something like a "read history"). And if 2 articles both have tag X, I want tag X to become more important.
So basicly, I'm looking for a way to do a common_tag_count match but with a weight for tags. Anyone has any idea how to accomplish this?
To get the tags used by the multiple articles, including how often they are used, you can use this query:
SELECT tag_id, COUNT(article_id) as tag_weight
FROM articles_tags
WHERE article_id IN ( /* Read articles */ 1, 2 )
GROUP BY tag_id;
To get the similar articles based on that selection you have to use above query in a similar join as you already have:
SELECT articles.article_id, articles.title, SUM(tag_weights.tag_weight)
FROM articles
JOIN articles_tags ON articles_tags.article_id = articles.article_id
JOIN (
SELECT tag_id, COUNT(article_id) as tag_weight
FROM articles_tags
WHERE article_id IN ( /* Read articles */ 1, 2 )
GROUP BY tag_id
) AS tag_weights ON articles_tags.tag_id = tag_weights.tag_id
WHERE articles.article_id NOT IN ( /* Read articles */ 1, 2 )
GROUP BY articles.article_id
ORDER BY SUM(tag_weights.tag_weight) DESC;
We're adding an extra JOIN here on the subquery which has access to the tag-weights. Using the ORDER BY you get the 'best' results first.
Demo: http://www.sqlfiddle.com/#!2/b35432/2/1
(articles 1 and 2 are read, giving tag 1 a weight of 2, tag 2 a weight of 1).
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
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
I am doing a pretty normal routine, but having a tough time getting my output correct.
I have two tables: *ads_list* (listings) and *ads_cate* (categories).
I am currently displaying my category list like so:
SELECT id, cateName FROM ads_cate ORDER BY cateName
What I am trying to achieve: count of all items in each category in this format:
Category | Number of Ads
categoryName 56
This is my current code, and have been tweaking but getting no output in my array:
SELECT
ads_cate.id,
ads_cate.cateName, // Category Name
ads_list.id,
ads_list.COUNT(title), // Title of ad
ads_list.Category // Relational Category ID INT(11)
FROM
ads_cate,
ads_list
GROUP BY cateName
ORDER BY cateName
I am calling in all required fields and running a COUNT() on my title field (as these are unique for each ad) and then I am grouping by cateName which also seems correct.
See what this gives you. I think it is what you need.
SELECT
ads_cate.cateName, // Category Name
COUNT(ads_list.id), // Title of ad
FROM
ads_cate
INNER JOIN
ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
ORDER BY cateName