I read this tutorial for a summary page of posts:
http://schoewilliam.fr/2015/02/10/jekyll-pro-tip-awesome-archive-page.html
It is a page of posts with the display determined by category and clever use of css pseudo properties.
It would be perfect for my site if it worked on pages or collections instead of posts.
I need it to work by category in a collection.
Collection 1
Category A
Category B
Category C
Category D
OR
A subset of categories for posts:
Group 1
Category A
--post 1
--post 2
Category B
--post 1
--post 2
Group 2
Category C
--post 1
--post 2
Category D
--post 1
--post 2
Can this be done in Jekyll? If anyone knows how or can point to a tutorial written for a beginner, I would appreciate the help.
Related
I have 2 tables in my MySQL one for the Books information and another table for the Books categories. Example:
books Table
id title
=== =========
1 Space Opera
2 Mars Attack
3 Mortal
4 Adventure Ride
book_category Table
id book_id category_id
=== ========= =========
1 10 2
2 24 3
3 10 8
4 7 1
If need a list of all books with their information from one category the query below will work fine
SELECT b.id, b.title
FROM books b
LEFT JOIN book_category c ON c.book_id=b.id
WHERE c.category_id = 2
However, my difficulty is that I don't know how to get a list of books that matches multiple categories (and ignoring all the other books that don't match 100% in all of those categories).
The idea is to have for example a list of books that are Sci-Fi and Mystery and Adventure, and ignoring all the other books that don't match exactly with this criteria.
Any idea how this can be done?
SELECT b.id, b.title
FROM books b
INNER JOIN book_category c ON c.book_id=b.id
WHERE c.category_id IN (2, 8) -- categories IDs list
GROUP BY b.id, b.title
HAVING COUNT(DISTINCT c.category_id) = 2 -- the amount of categories in the list
If the fields pair book_category (book_id, category_id) is unique (set by UNIQUE index) then DISTINCT in HAVING is excess.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Could anybody point me in the direction of how to write a complex mysql query? I am familiar with joins, views, and intermediate querying, but cannot quite conceptualise how to go about writing a query to pull out the data I am looking for.
I have three tables, user, user_category and category. user_category is just a bridge table.
user
user_id name
-----------------------
1 Bob
2 Tim
3 Dave
4 Simon
5 Ben
user_category
user_id category_id
-----------------------
1 1
1 5
1 3
2 4
2 1
3 2
3 4
4 4
4 1
5 3
5 5
category
category_id category
-----------------------
1 drawing
2 climbing
3 hunting
4 fishing
5 sleeping
So I can easily pull out a list of users in each category, no problem.
What I want to do, for each of these categories, is show a list of other categories that the list of users in this category is also in.
So if I take fishing as an example, my query would pull out Tim, Dave and Simon as being in that category. I want to list all the other categories that Tim Dave and Simon are in, with a count of how many users are in each of those categories. Like this:
Drawing (2) - this has Tim and Simon in it
Climbing (1) - this has Dave in it
I realise I need to get a list of users in a given category. Simple.
I also need a list of all the categories that each of these users is in (excluding current category).
Then for each category, I need a count of each of the users in each.
I think I could write this with separate nested queries, but I would like to write this in a single query using all the necessary joins if possible, but if anyone can point me towards some reading material or video content that would help me work out how to plan these queries, that would be even better.
You need multiple joins of the tables:
select cn.category, count(*) counter
from category c
inner join user_category uc on uc.category_id = c.category_id
inner join user u on u.user_id = uc.user_id
inner join user_category ucn
on ucn.user_id = u.user_id and ucn.category_id <> uc.category_id
inner join category cn on cn.category_id = ucn.category_id
where c.category = 'Fishing'
group by cn.category
See the demo.
Results:
| category | counter |
| -------- | ------- |
| climbing | 1 |
| drawing | 2 |
With this query, you get your data, if you want a specific hobby, add a where clause for c.category = 'fishing' for example
SELECT
MIN(c.category), COUNT(*), GROUP_CONCAT(`name`)
FROM
user_category uc
INNER JOIN
user u ON uc.user_id = u.user_id
INNER JOIN
category c ON uc.category_id = c.category_id
GROUP BY
uc.category_id
This returns the following result:
MIN(c.category) Count(*) GROUP_CONCAT(`name`)
drawing 3 Bob,Tim,Simon
climbing 1 Dave
hunting 2 Ben,Bob
fishing 3 Tim,Dave,Simon
sleeping 2 Bob,Ben
DBfiddle example
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=ad26cf2ab673d349f82d8875022af2aa
I have tried for a number of hours to get this. I am still quite new to mysql but have managed to achieve queries that I was impressed with after using the resources and examples I found. I am a bit stuck here. Apologies if I do not ask this very well.
Three tables that are used for managing categories and category membership within a project.
table a = project membership
id user_id project_id
== ======= ==========
1 1 10
2 1 12
3 3 45
4 5 12
table b = categories
id name project_id
== ==== ==========
1 cat1 10
2 cat4 12
3 cat8 45
tabke c = category members
id user_id_added category_id capability
== ============= =========== ==========
1 1 2 1
2 3 3 2
3 5 3 1
4 5 2 0
Required result
members of project 2
user_id category capability_in_category
======= ======== ======================
1 2 1
5 2 0
SELECT a.user_id
, c.capability
, b.id as category
FROM a
LEFT OUTER JOIN b
ON a.project_id = c.project_id
LEFT OUTER JOIN c
ON b.id = c.category_id
WHERE a.project_id = $project_id
AND c.category_id = $category_id;
It feels like I don't need to join the three tables, but I do not see a way of joining the project table with the category membership table without using the category table (b). The query I am running nearly works, but user capability is not returning correct. I am using left outer joins as a member may not always be part of a category, but they still need to be shown as a member in the project. I have been trying various joins and subqueries, without success. I basically need a list of the members in the project and if they are part of a category, to show the capability they have of the specific category. I feel there are a few ways of doing this potentially, but there is a gray area I am struggling to bridge.
The question is vague so I might help you to solve the wrong problem but if you want to have all members of a specific project listed (regardless of their capability) and to list the capabilities in a specified category listed as well, then:
SELECT project_memberships.user_id
, category_members.category_id AS category
, category_members.capability AS capability
FROM project_members
LEFT OUTER JOIN categories
ON project_members.project_id = categories.project_id
LEFT OUTER JOIN category_members
ON categories.id = category_members.category_id
AND category_members.user_id_added = project_membership.user_id
WHERE project_members.project_id = $project_id
AND (categories.id = $category_id OR categories.id IS NULL);
should get you that.
I altered tree things compared to your original query:
I used the table names as they are more speaking than "a, b, c"
I added the additional constraint category_members.user_id_added = project_membership.user_id to the second join so as to not join category_members of a different user to a project_members record.
I loosened the WHERE condition so that members not having the desired capability are also displayed. category and capability will be NULL for those records.
As to your question regarding having to join the three tables the answer is yes, you need to do that.
I'm new to programming and I'm currently trying to display Main categories + sub categories as a tree.
Menu with categories is already created. Now I want when I click on Main category name to load all products in all sub-categories assigned to this main category. The problem is I don't understand how to construct my query.
In my table categories I store both main cats and sub-cats. When I add sub-category to some main category I store main category ID under parentID column.
Example
ID Name parentID
426 Main Cat 0
427 sub-cat 426
428 sub-cat 2 426
429 Main cat 2 0
products table has categoryID column
productID categoryID
1 427
2 428
3 429
So when I lick on maincatID=1 it should show me products with id 1 and 2 because they are assigned to sub-cat which is assigned to main cat 1
This query I've tried
$masterCategory = '426';
$query="SELECT * from products
LEFT JOIN categories on categories.categoryID = products.categoryID
WHERE products.categoryID in (" . $masterCategory ."0)";
I should get 2 products when I click on catid=426 because I have two products added to subs which are assigned to catid=426 ... products 1 and 2.
The problem is that I've got 4-5 empty products with NULL everything.. which isn't correct.
Var_dump($query); return
SELECT * from products
LEFT JOIN categories on categories.categoryID = products.categoryID
WHERE products.categoryID in (427,428,426,0)
var_dump($masterCategory); return
string(12) "427,428,426,"
Note: I've read it about parameterized queries. I've adding parameter in sql just for a testing purpose.
Try to use the below query:
SELECT a.ID,c.productID,b.Name,b.parentID
FROM categories a JOIN categories b
ON(a.ID=b.parentID)
JOIN products c
ON(b.ID=c.categoryID)
WHERE a.ID=<catid>;
where catid is the main ID you have to pass.
I have simulated this with the data given above, and I am getting the results as I expected.
ID productID Name parentID
426 2 sub-cat 2 426
426 1 sub-cat 426
This should work
SELECT * from products
LEFT JOIN categories on categories.categoryID = products.categoryID
WHERE categories.categoryID = 426 OR categories.parendID=426;
These are my tables:
category (id(PK), name (varchar))
book_category (book_id(FK PK), category_id(FK PK)
book (id(PK), name(varchar))
Now I want to create a query where I can get every book from one category
I'm stuck here, I cant figure out how I can get data from more then one table, I tried a lot with INNER JOIN but get SQL errors all the time
for example:
SELECT * FROM book
INNER JOIN category
ON category.id = book.id
So my question is how can I use the book_category table?
What you have here is an intermediate table which allows you to have a many to many relationship. If you don't have the second table you couldn't have more than one book belonging to one category whilst also having the ability for more than one category to belong to one book.
E. G.
You have book 1, book 2, book 3 and category 1, category 2, category 3.
If a book can only belong to one category then you just have a foreign key in book which links to the primary key in category. E. G. Book 1 and book 2 belong to category 2.
But what if book 1 belongs to both category 2 and category 3? You can't have this relationship with just the 2 main tables because there's only 1 foreign key field.
Your second table now links the two main tables so your book table has a foreign key to a book id in the intermediate table and your category table has a foreign key to a category id in the intermediate table.
Now you can have these entries in the intermediate table.
1. Book 1 linked to category 2
2. Book 1 linked to category 3
3. Book 2 linked to category 2
Joining all three tables together will tell you that book 1 belongs to both category 2 and 3 and that book 2 belongs to category 2.
EDIT: adding an example to illustrate the point.
Book Table:
id name
1 A Best Seller
2 A Worst Seller
3 A Funny Horror
Category Table:
id name
1 Horror
2 Comedy
3 Non Fiction
Book-Category Table
book_id category_id
1 1
2 3
3 1
3 2
This structure says that book 1 belongs to the category Horror, book 2 belongs to Non Fiction and book 3 belongs to both Horror and Comedy.
The query, to find all books belonging to the Horror category is:
select book.name from
books join book_category on book.id = book_category.book_id
where book_category.category_id = 1
This finds every book name where the intermediate table has an entry for that book, belonging to the category with an id of 1.
You don't really need to join to the Category table at all unless
(a) you don't know the id of the category you are searching for (and thus need to search by category name, which wouldn't be recommended), or
(b) you want to include in the query some information about the category (such as its name, which might be important if you aren't limiting your query to a single Category)
Hope this helps.
Add categoryid into book table as a FK.
So your query will be like
select b.* from book as b inner join
category as c on b.categoryid=c.categoryid
and b.categoryid=#yourValue
Make changes in the table structure:
category : (id(PK), name (varchar))
book : (id(PK), name(varchar), categoryid(int)) // categoryid is FK of category table
Then use query:
SELECT b.*
FROM book AS b
INNER JOIN category AS c ON b.categoryid=c.id
Well, you need to use different JOIN:
SELECT b.* FROM book b
INNER JOIN book_category bc ON bc.book_id = b.id
WHERE bc.category_id = {DESIRED_VALUE}