Well i have two table, categories and items and i would like to get a random record of each of 10 random category.
tb_category
category_id PK
category_name
tb_items
item_id PK
category_id FK
My table tb_category has about 40 rows and tb_items has about 5k rows, i'm search performance.
SELECT * FROM
( SELECT c.category_id as cid, c.category_name, i.item_id FROM
tb_category c INNER JOIN
tb_items i ON c.category_id = i.category_id ORDER BY RAND() ) AS ShuffeledItems
GROUP BY ShuffeledItems.cid limit 10
I don't know if that is better way to do it.
Thanks.
I would be inclined to use a correlated subquery:
select c.*,
(select i.item_id
from items i
where i.category_id = c.category_id
order by rand()
limit 1
) as item_id
from tb_category c
order by c.id
limit 10;
Related
I'm joining two tables to display car brands. Here is the structure:
SELECT DISTINCT
b.title
FROM
brands as b
INNER JOIN items as i
ON i.brand_id = b.id
WHERE i.status = 1
ORDER BY COUNT(i.brand_id) DESC;
The above only produces one record. If I remove "ORDER BY COUNT(i.brand_id) DESC;" it displays all the records correctly.
I would like to sort result based on number of vehicles under each brand category. So for example if bmw category has the most car listed under, it should be the first one.
SELECT b.title
FROM brands as b
INNER JOIN items as i
ON i.brand_id = b.id
WHERE i.status = 1
GROUP BY b.title
ORDER BY COUNT(i.brand_id) DESC;
This should work for you.
I would use
SELECT b.title, count(i.brand_id)
FROM items i
LEFT JOIN brands as b
ON b.id = i.brand_id
WHERE i.status = 1
GROUP BY i.brand_id
ORDER BY COUNT(i.brand_id) DESC;
Your concern is the amount of cars in inventory. You want to break them down by how many of each brand you have. So, you're mainly concerned with the items tables and only need the brands table to get the information stored in the items table (brand name). Lastly, in order to get aggregate the number of brands of each, you must let MySQL know what you want in the GROUP BY.
I haven't used an EXPLAIN, but I would think the bottom query is more efficient.
You could join brands on an aggregate query:
SELECT b.title
FROM brands b
INNER JOIN (SELECT brand_id, COUNT(*) AS cnt
FROM items
WHERE status = 1
GROUP BY brand_id) i ON i.brand_id = b.id
ORDER BY cnt DESC;
I have a problem putting a limit on the number of rows from my Jokes table.
This is my query working, getting all rows:
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Would it be something like?
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM (
SELECT * FROM Jokes ORDER BY ID DESC Limit 0,40)
AS a
LEFT JOIN Categories
AS b
ON a.CategoryID = b.ID
why not using
SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText
FROM Jokes
LEFT JOIN Categories
ON Jokes.CategoryID = Categories.ID
ORDER BY Jokes.ID DESC
Limit 0,40
I have got 2 tables: categories and products.
Categories have parent-child relationship structure and the data is fetched by joining the same table.
When fetching the data, I have to count:
how many products each category contains which have stock >= 1
how many subcategories a category contains which contain at least 1 product with stock >= 1
SELECT c. * , count( DISTINCT s.cat_id ) AS number_of_subcategories, count( DISTINCT p.id ) AS number_of_products FROM categories c
LEFT JOIN categories s
ON s.parent_id = c.cat_id
LEFT JOIN products p
ON p.cat_id = c.cat_id AND p.stock >= 1
GROUP BY c.cat_name
ORDER BY number_of_products ASC
At the first glance all goes well, but unfortunately I get total number of all subcategories.
Do I miss one more join or what is my problem so far?
Here is the code: SQLFiddle
You could alter your query to use a subquery to get the number of subcategories similar to this:
SELECT c.cat_id,
c.parent_id,
c.cat_name,
count(sp.cat_id) AS number_of_subcategories,
count(DISTINCT p.id) AS number_of_products
FROM `categories` c
LEFT JOIN
(
select distinct s.cat_id, s.parent_id
from categories s
inner join products p
on s.cat_id = p.cat_id
where p.stock > 1
) sp
ON sp.parent_id = c.cat_id
LEFT JOIN products p
ON p.cat_id = c.cat_id
AND p.stock >= 1
GROUP BY c.cat_id, c.parent_id, c.cat_name;
See SQL Fiddle with Demo
Try changing AND for WHERE. Does it work?
Francisco
I got 2 tables:
1 Category
- cID
- Name
- Active
2 Products
- pID
- Name
- category_id
- active
This is want i want:
I want a list from categories that ONLY have ACTIVE products.
Thanks in advance
SOLUTION:
SELECT DISTINCT category.* FROM category INNER JOIN products ON category.id = products.c_id WHERE products.active = 0 ORDER BY category.id DESC
Assume Products.Active = 1 means active state.
SELECT c.*
FROM Category c
INNER JOIN Products p ON c.CID = p.category_id
GROUP BY c.CID
HAVING COUNT(*) = SUM(IF(p.Active = 1, 1, 0))
I'd suggest construction like this
IMHO, it's syntax shows what you really want to see - all categories that have active products
select C.cID, C.Name, C.Active
from Category as C
where C.cID in (select distinct T.category_id from Products as T where T.Active = 1)
SELECT * FROM Products
Join Category on Category.ciD = Products.category_id
WHERE Category.Active = 1 AND Products.Active =1
This is assuming your active columns are just 1 or 0. Otherwise adjust according to what values you're storing there.
pls can anybody help me width this query?
I cannot find solution for it.
SELECT *,p.cat_id, cat_name FROM (
SELECT cat_id
FROM categories
ORDER BY cat_id
LIMIT 700000, 10
) o
JOIN categories p
ON p.cat_id = o.cat_id
ORDER BY p.cat_id
It is very fast on table with 800 000 records, but what I need is to sort data with order by clause and where claue,too - for paging. If I use order by it si very slow or result is not ordered correctly>
SELECT *, p.cat_id, cat_name FROM(
SELECT cat_id
FROM categories
LIMIT 700000, 10
) o JOIN categories p
ON p.cat_id = o.cat_id
ORDER BY p.cat_name
page 1
LIMIT 700000,5:
id name
12525525 car
15155151 carpet
1521512i zone
page 2
LIMIT 700005,5
id name
12525525 carefull
15155151 excellent
52151222 drive
I need result:
page 1 car
carpet
drive
excellent ... etc.
f.e. , this is very slow ofcourse >
SELECT *, p.cat_id, cat_name
FROM (
SELECT cat_id
FROM categories
**ORDER BY cat_name**
LIMIT 700000, 10
) o
JOIN categories p
ON p.cat_id = o.cat_id
ORDER BY p.cat_name
primary key cat_id, autoincrement
I have indexes on fields in table
Many thanks for help or some ideas
You didn't need to JOIN the same table, and you have to do the ORDER BY in an inner query to be ordered correctly with the WHWRE clause in the outer one:
SET #rownum = 0;
SELECT t.*, t.rank as TableRank
FROM
(
SELECT *, (#rownum := #rownum + 1) as rank
FROM categories c
ORDER BY c.cat_name
) t
WHERE rank BETWEEN ((#PageNum - 1) * #PageSize + 1)
AND (#PageNum * #PageSize)