MYSQL OR SQL SELECT with both COMPARE (HAVING) and GROUP - php

I have three tables in a database and want to sum and compare in one SELECT statement.
The logic is that if one article runs low in stock it should be selected. An article runs low if the summarized quantity of all rows with the same artID in the table Articles in stock (an article can have several rows in the table and therefore needs to be summarized) is lower or equal to the column warning in the row with the corresponding ID. This is also warehouse specific, meaning that an article can be fully stocked in one warehouse, and at the same time running low in another. Which is why it is needed to summarize grouped on a specific warehouse.
Information about which warehouse is running low, and such as artNr for the article is also needed, which can be found in table Articles
Articles
+----+-------+---------+
| ID | artNr | warning |
+----+-------+---------+
| 1 | LA08 | 5 |
| 2 | LA09 | 10 |
| 3 | LA58 | 0 |
+----+-------+---------+
warehouse
+----+-------+
| ID | artID |
+----+-------+
| 3 | 1 |
| 4 | 1 |
| 5 | 2 |
+----+-------+
Articles in stock
+----+-------+-------------+----------+
| ID | artID | warehouseID | quantity |
+----+-------+-------------+----------+
| 1 | 1 | 3 | 10 |
| 2 | 1 | 5 | 15 |
| 3 | 2 | 5 | 45 |
| 4 | 1 | 3 | 20 |
| 5 | 3 | 5 | 4 |
+----+-------+-------------+----------+

select a.id, s.warehouseID, sum(s.quantity)
from articles a
join articles_in_stock s on a.id = s.artID
group by a.id, s.warehouseID
having sum(s.quantity) <= a.warning

Related

mysql optimisation number of ads in a category

I have a personal ads website .
First problem: I have 2 tables, category and ads.
Category table (id is the primary key)
| id | type | subtype | pos |
+-----+-------------+-----------+--------+
| 1 | sell | | 1 |
| 2 | jobs | | 2 |
| 3 | dating | | 3 |
| 4 | dating | boys | 1 |
| 5 | dating | girls | 2 |
| 6 | sell | cars | 1 |
| 7 | jobs | teacher | 1 |
Ads table (id is the primary key) - this table has a lot of ads
| id | title | type | subtype|
+-----+-------------+-----------+--------+
| 1 | some text | sell | cars |
| 2 | some text | dating | girls |
| 3 | some text | dating | boys |
I want to read all category types and get the total number of ads for that type, and I need to get the subtypes too.
The output on my page will be something
Sell (1 ads)
Cars
Jobs (0 ads)
Teacher
Dating (2 ads)
Boys
Girls
What I am currently doing is making 3 queries using php and mysql, the first of which returns the categories type is a select where subtype="" , the second counts existing ads in those categories and the third gives me subtype for each type, I want to optimize mysql, reduce the number of queries and be faster even if it means changing the tables and adding more indexes
Select query:
select *
from category
where subtype = ''
order by pos desc
Count numbers of ads on a type:
select type
from ads
where type = '$type_name'
Select subtype:
select *
from category
where type = '$type_name'
and subtype !=''
order by pos
You can use this
SELECT c.*,a.c_ads
FROM
category c LEFT JOIN ( SELECT COUNt(type) c_ads,type FROM ads GROUP BY type) a
ON c.type = a.type
ORDER BY c.type,c.pos DESC
id | type | subtype | pos | c_ads
-: | :----- | :------ | --: | ----:
3 | dating | | 3 | 2
5 | dating | girls | 2 | 2
4 | dating | boys | 1 | 2
2 | jobs | | 2 | null
5 | jobs | teacher | 1 | null
1 | sell | | 1 | 1
5 | sell | cars | 1 | 1
see fiddle
When you get the data in php check row by row, if the type changes and then you can can use the column c_ads to Display ($row{'c_ads'] ads) and row['type'] to display the text of the category
Where row['type'] doesn't change you find the subcategories to row['type'] in row['subtype ']

How do I compare multiple row values to get a percentage? [duplicate]

Let's say I need to have the ratio of "number of items available from certain category" to "the number of all items". Please consider a MySQL table like this:
/*
mysql> select * from Item;
+----+------------+----------+
| ID | Department | Category |
+----+------------+----------+
| 1 | Popular | Rock |
| 2 | Classical | Opera |
| 3 | Popular | Jazz |
| 4 | Classical | Dance |
| 5 | Classical | General |
| 6 | Classical | Vocal |
| 7 | Popular | Blues |
| 8 | Popular | Jazz |
| 9 | Popular | Country |
| 10 | Popular | New Age |
| 11 | Popular | New Age |
| 12 | Classical | General |
| 13 | Classical | Dance |
| 14 | Classical | Opera |
| 15 | Popular | Blues |
| 16 | Popular | Blues |
+----+------------+----------+
16 rows in set (0.03 sec)
mysql> SELECT Category, COUNT(*) AS Total
-> FROM Item
-> WHERE Department='Popular'
-> GROUP BY Category;
+----------+-------+
| Category | Total |
+----------+-------+
| Blues | 3 |
| Country | 1 |
| Jazz | 2 |
| New Age | 2 |
| Rock | 1 |
+----------+-------+
5 rows in set (0.02 sec)
*/
What I need is basically a result set resembles this one:
/*
+----------+-------+-----------------------------+
| Category | Total | percentage to the all items | (Note that number of all available items is "9")
+----------+-------+-----------------------------+
| Blues | 3 | 33 | (3/9)*100
| Country | 1 | 11 | (1/9)*100
| Jazz | 2 | 22 | (2/9)*100
| New Age | 2 | 22 | (2/9)*100
| Rock | 1 | 11 | (1/9)*100
+----------+-------+-----------------------------+
5 rows in set (0.02 sec)
*/
How can I achieve such a result set in a single query?
Thanks in advance.
SELECT Category, COUNT(*) AS Total , (COUNT(*) / (SELECT COUNT(*) FROM Item WHERE Department='Popular')) * 100 AS 'Percentage to all items',
FROM Item
WHERE Department='Popular'
GROUP BY Category;
I'm not sure of the MySql syntax, but you can use a sub-query as shown.
This should do it:
SELECT I.category AS category, COUNT(*) AS items, COUNT(*) / T.total * 100 AS percent
FROM Item as I,
(SELECT COUNT(*) AS total FROM Item WHERE Department='Popular') AS T
WHERE Department='Popular'
GROUP BY category;
SET #total=0;
SELECT Category, count(*) as Count, count(*) / #total * 100 AS Percent FROM (
SELECT Category, #total := #total + 1
FROM Item
WHERE Department='Popular') temp
GROUP BY Category;
An advantage of doing it this way is you do not have to duplicate the WHERE condition, which is a ticking time bomb the next time someone comes by to update the condition, but doesn't realize it's in two different places.
Avoiding the duplicate WHERE condition also improves readability, especially if your WHERE is more complex (with multiple joins, etc).

Find the ranking for Concatenated rows in mysql

I have a table with concatenated values within both rows, I am therefore uniquely retrieve ranking for each row in the tables.
UPDATE
The other tables has been added to question
NamesTable
NID | Name |
1 | Mu |
2 | Ni |
3 | ices |
GroupTable
GID | GName |
1 | GroupA |
2 | GroupB |
3 | GroupC |
MainTable
| NID | Ages | Group |
| 1 | 84 | 1 |
| 2 | 64 | 1 |
| 3 | 78 | 1 |
| 1 | 63 | 2 |
| 2 | 25 | 2 |
| 3 | 87 | 2 |
| 1 | 43 | 3 |
| 2 | 62 | 3 |
| 3 | 37 | 3 |
Now the first Name is equated to the first age in the table, I am able to equate them using php and foreach statements, Now the problem is with the ranking of the ages per each group. I am ranking the names uniquely on each row or group.
Results which is expected
| Names | Ages | Group | Ranking |
| Mu,Ni,ices | 84,64,78 | 1 | 1,3,2 |
| Mu,Ni,ices | 63,25,87 | 2 | 2,3,1 |
| Mu,Ni,ices | 43,62,37 | 3 | 2,1,3 |
In my quest to solving this, I am using GROUP_CONCAT, and I have been able to come to this level in the below query
SELECT
GROUP_CONCAT(Names) NAMES,
GROUP_CONCAT(Ages) AGES,
GROUP_CONCAT(Group) GROUPS,
GROUP_CONCAT( FIND_IN_SET(Ages, (
SELECT
GROUP_CONCAT( Age ORDER BY Age DESC)
FROM (
SELECT
GROUP_CONCAT(Ages ORDER BY Ages DESC ) Age
FROM
`MainTable` s
GROUP by `Group`
) s
)
)) rank
FROM
`MainTable` c
GROUP by `Group`
This actually gives me the below results.
| Names | Ages | Group | Ranking |
| 1,2,3 | 84,64,78 | 1 | 7,9,8 |
| 1,2,3 | 63,25,87 | 2 | 5,6,4 |
| 1,2,3 | 43,62,37 | 3 | 2,1,3 |
The only problem is that the ranking Values increase from 1 to 9 instead of ranking each row uniquely. Its there any idea that can help me cross over and fix this? I would be grateful for your help. Thanks

MySQL - PHP unique values from rows

I have this table:
table 1
+----+-----------------------+----------+------+-------+
| ID | COUNTRY | QUANTITY | EACH | PRICE |
+----+-----------------------+----------+------+-------+
| 1 | U.S.A | 1 | 12 | 1*12 |
| 2 | U.K. | 2 | 3 | 2* 3 |
| 3 | GERMANY | NULL | 3 | |
| 4 | FRANCE;GERMANY; U.S.A | 0 | 7 | |
| 5 | U.S.A;GERMANY | 3 | 8 | 3*8 |
| 6 | FRANCE;U.K. | 1 | 10 | 1*10 |
| 7 | U.S.A;FRANCE | 2 | 6 | 2*6 |
| 8 | FRANCE;FRANCE | 9 | 3 | 9*3 |
+----+-----------------------+----------+------+-------+
and this code sql:
SELECT
COUNTRY,
SUM(COALESCE(IF(QUANTITY = NULL OR QUANTITY = 0,1,QUANTITY), 1) * EACH) AS PRICE
FROM table1
GROUP BY COUNTRY
How could I make unique values for the country column and return: USA = 48 (ID: 1+5+7); UK= 6; GERMANY=3; FRANCE = 44 (ID: 4+6+8). I want that the rows, those contain two, three, four countries to be eliminated and to remain only the first country from row.
Thank you!
Use substring_index to get the first country in the ;-separated list.
SELECT
SUBSTRING_INDEX(COUNTRY, ';', 1) AS COUNTRY
SUM(IF(QUANTITY IS NULL OR QUANTITY = 0,1,QUANTITY) * EACH) AS PRICE
FROM table1
GROUP BY COUNTRY
It would be much more complicated if you wanted to keep all the rows where each country appears (in that case I would recommend doing it in PHP, not MySQL, since MySQL doesn't have a builtin way to do explode()).

MySQL MIN() limit by column

I have a MySQL table that is formatted as follows:
group_clue:
---------------------------------------------------
| id | group_id | clue_id | completed | run_order |
---------------------------------------------------
| 1 | 1 | 2 | 1 | 1 |
| 2 | 1 | 6 | 1 | 2 |
| 3 | 1 | 4 | 1 | 3 |
| 4 | 1 | 3 | 0 | 4 |
| 5 | 1 | 1 | 0 | 5 |
| 6 | 1 | 5 | 0 | 6 |
| 7 | 2 | 9 | 1 | 1 |
| 8 | 2 | 2 | 0 | 2 |
...
---------------------------------------------------
The data above in the group_clue is constructed such that each group_id has every clue_id at some run_order (ranging from 1 to the number of clue_ids and not repeating for a particular group).
First Question
I want to create a table showing the first clue_id for each group_id where completed = 0 when ordered by run_order (aliased as current_clue). Using the above example, this would give:
---------------------------
| group_id | current_clue |
---------------------------
| 1 | 3 |
| 2 | 2 |
---------------------------
My preliminary attempt is:
SELECT group_id, MIN(clue_id) as current_clue
FROM group_clue
WHERE completed = 0
GROUP BY group_id
However, this returns the same clue_id for each group_id.
Second Question
From the data in the first question, I would like to compose a final table where I GROUP_CONCAT() these results so that it contains every current_clue and each group_id that contains that current_clue. I would also like it ordered from those clues with the most group_ids to those with the fewest. An example resulting table is:
--------------------
| clue | group_ids |
--------------------
| 3 | 1,5,4,3 |
| 2 | 2,6 |
--------------------
I cannot figure out the ordering. My preliminary attempt is:
SELECT clue_id, GROUP_CONCAT(group_id)
FROM [resulting_table]
GROUP BY clue_id
ORDER BY [something]
Any help is appreciated: what queries would fit this scenario?
The first part of your question can be solved this way (it expects that run_order is unique per group):
SELECT t1.group_id,
t1.clue_id AS current_clue
FROM group_clue t1
INNER JOIN (SELECT group_id,
MIN(run_order) as run_order
FROM group_clue
WHERE completed = 0
GROUP BY group_id) t2 USING (group_id, run_order)
The logic of this query is pretty simple:
The inner query selects the pairs of group_id and the corresponding minimal value of run_order which has the completed = 0.
After that we join the original table to this set of pairs so that we could select the corresponding clue_id additionally.
You can sort by number of elements per group using
ORDER BY COUNT(*) DESC

Categories