I want to select the rows from products table.
The products are season based.
each product row/entity contains a column named id_season
and seasons table looks like
id | season_name | active | created | modified
Season names are Year like 2016,2017,2018 ...
I want to select all the products from 2016 and 2017 which have same code
I have a simple select like
SELECT *
FROM products P
INNER JOIN seasons S ON S.id = P.id_season
WHERE S.active = 1
AND S.season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 )
but don't know how to refine it, to match the codes on products from different seasons.
Try with the below code..
;WITH cte_1
AS
(SELECT *,COUNT(p.code) OVER(partition by p.code Order by p.code) cnt
FROM products P
INNER JOIN seasons S ON S.id = P.id_season
WHERE S.active = 1
AND S.season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 )) -- or simply put IN ('2016','2017')
SELECT *
FROM cte_1
WHERE cnt>1
or you can use a subquery format as below.
SELECT *
FROM
(SELECT *,COUNT(p.code) OVER(partition by p.code Order by p.code) cnt
FROM products P
INNER JOIN seasons S ON S.id = P.id_season
WHERE S.active = 1
AND S.season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 )) t
WHERE t.nt>1
You can use inner query as follows:
SELECT * FROM products
where id_season in (Select id from seasons where
season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 ));
Related
I have three tables:
products:
id name
1 juice
2 chips
3 water
orders:
id product_id order_id
1 1 special1
2 3 special1
3 2 special1
4 1 special2
5 2 special2
final_orders:
id order_id date
1 special1 25-3-2017
2 special2 25-3-2017
I want to select all products names in every order using order_id to show:
ID: Special1
Date: 25-3-2017
Products List:
juice
water
chips
ID: Special2
Date: 25-3-2017
Products List:
juice
chips
I use this:
$sql = "select * from products,orders where products.id = orders.product_id";
but it doesn't work and show me duplicated results.
thank you.
You need to join with final_orders as well:
SELECT *
FROM final_orders AS f
JOIN orders AS o ON f.order_id = o.order_id
JOIN products AS p ON p.id = o.product_id
ORDER BY f.order_id
To prevent duplication in the output, your loop that prints the output should only show the information from final_orders when it changes. See How can i list has same id data with while loop in PHP?
If you want to see one final order per record in your result set, then you will have to aggregate the products which appear in each order. One option then is the following query which aggregates order products into CSV using MySQL's GROUP_CONCAT():
SELECT t1.order_id,
t1.date,
t2.products
FROM final_orders t1
INNER JOIN
(
SELECT a.order_id, GROUP_CONCAT(b.name) AS products
FROM orders a
INNER JOIN products b
ON a.product_id = b.id
GROUP BY a.order_id
) t2
ON t1.order_id = t2.order_id
Demo here:
Rextester
I have Three tables look like below:
test_case
id project_id requirement_id
1 5 11,12
2 4 12,13
3 5 10,12
task_categories (refenced with test_case table with requirement_id)
id name
10 ten
11 eleven
12 twelve
13 thirtien
projects (refenced with test_case table with project_id)
id name
4 P1
5 P2
Now,i wanna make a query with where condition by passing parameter like project_id=5 and want the output look like below:
id project_name requirement_name
1 P2 eleven,twelve
3 P2 ten,twelve
I tried the following code in my model:
public function display($project_id) {
$sql = "
SELECT i.id as id, i.project_id as project_id, requirement_id, GROUP_CONCAT(c.name SEPARATOR '\n <br>*') as req_name, p.id as projects_id FROM test_case i, task_categories c, projects p
WHERE FIND_IN_SET(c.id, i.requirement_id) AND i.project_id = $project_id
GROUP BY i.id";
$query = $this->db->query($sql);
return $query->result();
}
Your current approach has a problem because it results in selecting non aggregate columns while using GROUP BY. Here is a query which should work:
SELECT t1.id,
COALESCE(t2.project_name, 'NA') AS project_name,
t1.req_name
FROM
(
SELECT t.id,
t.project_id,
GROUP_CONCAT(tc.name SEPARATOR '\n <br>*') AS req_name
FROM test_case t
INNER JOIN task_categories tc
ON FIND_IN_SET(tc.id, t.requirement_id) > 0
GROUP BY t.id, t.project_id
) t1
LEFT JOIN projects t2
ON t1.project_id = t2.id
In the above query, I join test_case and task_categories together in a subquery to obtain all combinations of id, project_id, and their requirements list. Then I use another LEFT JOIN to bring in the project name from the projects table.
I want to show post from users that specified user is followed and i have two tables at below. but its query is very slow.
table user
id | username
1 | name1
2 | name2
3 | name3
..
..
table post
id | poster_id | post_content
1 | 2
2 | 3
3 | 10
..
..
table follow
followerid | followtoid
1 | 2
1 | 3
2 | 10
..
..
Assume that all tables have more than 1000 rows.
This's SQL
SELECT *
FROM post
WHERE poster_id IN (
SELECT followtoid
WHERE followerid = $_SESSION['userid']
)
And this's the second cast is very slow too.
I want to list all member by order from their total posts.
SELECT *
FROM user
ORDER BY (
SELECT COUNT(id)
FROM post
WHERE post_id = user.id
) DESC;
Try indexing post.userid, post.poster_id, followtoid.followerid and user.user_id, using CREATE INDEX, and use LEFT JOIN clause on your queries instead:
SELECT *
FROM user u
LEFT JOIN SELECT poster_id, COUNT(*) as count FROM post p GROUP BY poster_id
ON (u.user_id = p.poster_id)
ORDER BY count DESC;
and:
SELECT * FROM post AS p
LEFT JOIN (SELECT followerid FROM followtoid) AS f
ON (p.userid=f.followerid)
WHERE p.userid = {$_SESSION['userid']}
Use a JOIN for the first query
SELECT p.*
FROM post p
JOIN follow f ON p.post_id = f.followtoid
WHERE f.followerid = $_SESSION['userid']
and a JOIN plus a GROUP BY for the second
SELECT u.*, tbl.postCount
FROM user u
JOIN (
SELECT poster_id, COUNT(*) AS postCount
FROM post p
GROUP BY posterID
) tbl ON tbl.poster_id = u.id
ORDER BY postCount DESC
You can accomplish the second query without a subquery:
SELECT u.*, COUNT(p.poster_id) as postCount
FROM user u
LEFT JOIN post p
ON (u.user_id = p.poster_id)
GROUP BY u.user_id
ORDER BY postCount DESC;
Is it possible to add and multiply the count of different tables where the id is the same?
Imagine:
Table_1 Table_2 Table_3
id id id
1 1 1
1 2 2
2 2 3
3 2 3
3 2 3
3 3 3
So that the end result would be this table with 2 columns:
id (COUNT(Table_1.id) + 2*COUNT(Table_2.id) + 3*COUNT(Table_3.id))
1 7
2 12
3 17
I don't know if I understood you correctly but give this a try,
SELECT a.ID,
a.aa + (2 * b.bb) + (3 * c.cc)
FROM
(
SELECT ID, COUNT(*) aa
FROM table1
GROUP BY ID
) a LEFT JOIN
(
SELECT ID, COUNT(*) bb
FROM table2
GROUP BY ID
) b ON a.ID = b.ID
LEFT JOIN
(
SELECT ID, COUNT(*) cc
FROM table3
GROUP BY ID
) c ON a.ID = c.ID
SQLFiddle Demo
SELECT id, counts_1.number + 2 * counts_2.number + 3 * counts_3.number
FROM
(SELECT id, COUNT(*) AS number FROM Table_1 GROUP BY id) AS counts_1
JOIN
(SELECT id, COUNT(*) AS number FROM Table_2 GROUP BY id) AS counts_2 USING (id)
JOIN
(SELECT id, COUNT(*) AS number FROM Table_3 GROUP BY id) AS counts_3 USING (id)
Note that this solution requires that every id exists at least once in each of the tables, otherwise it will be left out of the result. Changing this would require a FULL OUTER JOIN that MySQL is incapable of. There are ways around that limitation, though.
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)