How to show PHP MySQLi having count result in webpage? [duplicate] - mysql

This question already has answers here:
select count(*) from table of mysql in php
(12 answers)
Closed 3 years ago.
I can fetch the desired record using MySQLi query directly in the database. But how to reflect the count in the webpage as it shows data in the result-set of phpMyAdmin interface?
Below is the MySQLi query I have used to fetch the below detail.
SELECT `recordUniqueID`, COUNT(*) FROM `status_data` WHERE
`status_data`.`statusName` = '1'
AND `status_data`.`emailAddress` = 'shashibhushan.roy#abcd.com'
AND `status_data`.`recordCreatedDateTime` BETWEEN '2018/11/25 10:00' AND '2019/03/02 10:00'
GROUP BY `recordUniqueID`
HAVING COUNT(*) >= 1
ORDER by id ASC
Below is the result I got in phpMyAdmin interface
Below is the screen from my webpage where I want to show the count of respective rows.
The yellow space I want to fill with the count of the respective rows (repeat values).
https://i.stack.imgur.com/9UeDn.png
https://i.stack.imgur.com/Dmy2w.png

You could add an alias to you count column (eg: my_count)
SELECT `recordUniqueID`, COUNT(*) my_count
FROM `status_data`
WHERE `status_data`.`statusName` = '1'
AND `status_data`.`emailAddress` = 'shashibhushan.roy#abcd.com'
AND `status_data`.`recordCreatedDateTime` BETWEEN '2018/11/25 10:00' AND '2019/03/02 10:00'
GROUP BY `recordUniqueID`
HAVING COUNT(*) >= 1 ORDER by id ASC
and then you could echo the value accessing using the alias
assumin the resulting rows are returned in a $row var :
echo $row['my_count'];

Related

How to count the length of column seperated by ',' after using group_concat [duplicate]

This question already has answers here:
How to count items in comma separated list MySQL
(6 answers)
Closed 3 years ago.
I have a table looks like below:
ID path
| 1 YouTube,Newsletter,Social
| 2 YouTube,Newsletter
| 3 YouTube
Now I want to create a column to count the length of the path column. such as below:
ID path count weights
| 1 YouTube,Newsletter,Social 3 0.33
| 2 YouTube,Newsletter 2 0.5
| 3 YouTube 1 1
How do I do this?
I have tried JSON_LENGTH but couldn't get the command working.
PS. essentially I'm trying to replicate a query in PostgreSQL:
' select user_id, channels, 1.0 / array_length(channels, 1) as weights
from (
// ... query for marketing_channels as before)'
I am using MYSQL.
select d.email_entry_id
,d.channels
,JSON_LENGTH(d.channels)
from (
select email_entry_id
,group_concat(attribution_string order by visit_date asc separator ',' ) as channels
from database) d
error message: Error Code: 1370. execute command denied to user 'yb'#'%' for routine 'company.JSON_LENGTH'
Hope the question is clear enough. let me know if i need to clarify anything.
If I followed you correctly, you could simply extend the logic of your existing query (which, by the way, seems to be missing a GROUP BY clause). Instead of querying the aggregated data, it would be simpler to start from the original data, like:
SELECT
email_entry_id,
GROUP_CONCAT(attribution_string ORDER BY visit_date SEPARATOR ',' ) as channels,
COUNT(*) as `count`,
1/COUNT(*) as weight
FROM database
GROUP BY email_entry_id
There is a very common trick to achieve such outcome, demonstrated by following query
SELECT ID, PATH,
(LENGTH(PATH) - LENGTH(REPLACE(PATH, ',', ''))) + 1 COUNT
FROM DATABASE /* OR WHATEVER IS THE TABLE NAME */
The result

MySQL COUNT() GROUP BY doesn't work

I have a problem in counting by MySQL in a GROUP BY
This is the query that does not return the desired result.
SELECT COUNT(bagno)
FROM disposizione_assegnazione_pezze
JOIN pezze
ON pezza = id
WHERE id_prodotto_tessuto = 12096
AND id_collezione = 11
AND id_stagione = 22
AND id_tema = 1
GROUP BY bagno
The result of the count is 3
This is the pezza table and its primary key is id
This is the table disposizione_assegnazione_pezze that has the pezza column which refers to the previous table
Why does not return 1 as a result my query?
Question of the problem
I want to count how many different bagno are there
I dont think you need GROUP BY, instead use DISTINCT
SELECT COUNT(DISTINCT bagno)
SQL DEMO
Check your query without agregatted function COUNT/GROUP BY
As you can see bagno = 55 appear three times, that is why when you group by bagno and count get 3.

MYSQL retrieve data dependent on rows returned

I am working on a mysql query that will filter out certain occurrences dependent on how many rows are returned.
I am trying to filter out any support categories when the number of rows returned are 1, however leave the support category in when the result set turned is more than 1.
I originally had this idea however it seems as if it will not work.
SELECT stockmaster.description, SUM(salesorderdetails.quantity), stockmaster.categoryid as qty
FROM salesorderdetails, stockmaster
where salesorderdetails.stkcode=stockmaster.stockid
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M' THEN COUNT(*) >= 2 ELSE COUNT(*) = 1 END
Any help will be gratefully accepted.
Try this
SELECT *
FROM
(
SELECT stockmaster.description,
SUM(salesorderdetails.quantity),
stockmaster.categoryid as qty ,
COUNT(*) AS count
FROM salesorderdetails, stockmaster
where salesorderdetails.stkcode=stockmaster.stockid
and orderno='5222'
group by stockmaster.description
HAVING CASE WHEN stockmaster.categoryid = 'S&M'
) MAIN_DATA
WHERE MAIN_DATA.count >1

Return n number of rows from SQL Server query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Row Offset in SQL Server
I have a query which has a like condition and it returns around 1500 rows. Now I want to know how can I get the rows part by part, e.g. return 75 rows, then again the next 75 rows until it reaches the end of the rows. I know I can use something like SELECT TOP 75
I tried something like this but it does not return any rows
SELECT *
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY WarehouseSubType.id) as row,
WarehouseSubType.id,
WarehouseType.name as WarehouseTypename,
WarehouseType.alternateName AS WarehouseTypealternateName,
WarehouseSubType.name AS WarehouseSubTypename,
Warehouse.alternateName AS WarehousealternateName,
WarehouseSubType.alternateName AS WarehouseSubTypealternateName,
WarehouseSubType1.name AS WarehouseSubType1name,
WarehouseSubType1.alternateName AS WarehouseSubType1alternateName,
Warehouse.alternateName AS Warehousename,
Branch.name AS Branchname,
Branch.alternateName AS BranchalternateName,
WarehouseProductQuantity.actualQuantity,
WarehouseProductQuantity.reservedQuantity,
Supplier.companyName,
Supplier.companyNameAlternate,
Tafsil.description,
Tafsil.alternateDescription,
(WarehouseProductQuantity.actualQuantity - WarehouseProductQuantity.reservedQuantity) AS quantity
FROM WarehouseSubType
INNER JOIN WarehouseType ON (WarehouseSubType.warehouseTypeId = WarehouseType.id)
INNER JOIN WarehouseSubType1 ON (WarehouseSubType.id = WarehouseSubType1.warehouseSubTypeId)
) a
WHERE
warehouseTypename like '%Ve%'
AND row > 0 and row < 75
The code you posted should not even compile, let alone return rows. The sub query is not closed and row would not be recognised in the inner query.
Try this instead:
SELECT * FROM
( SELECT ROW_NUMBER() OVER (ORDER BY WarehouseSubType.id) as row,
WarehouseSubType.id,
WarehouseType.name as WarehouseTypename,
WarehouseType.alternateName AS WarehouseTypealternateName,
WarehouseSubType.name AS WarehouseSubTypename,
Warehouse.alternateName AS WarehousealternateName,
WarehouseSubType.alternateName AS WarehouseSubTypealternateName,
WarehouseSubType1.name AS WarehouseSubType1name,
WarehouseSubType1.alternateName AS WarehouseSubType1alternateName,
Warehouse.alternateName AS Warehousename,
Branch.name AS Branchname,
Branch.alternateName AS BranchalternateName,
WarehouseProductQuantity.actualQuantity,
WarehouseProductQuantity.reservedQuantity,
Supplier.companyName,
Supplier.companyNameAlternate,
Tafsil.description,
Tafsil.alternateDescription,
(WarehouseProductQuantity.actualQuantity - WarehouseProductQuantity.reservedQuantity) AS quantity
FROM WarehouseSubType
INNER JOIN WarehouseType
ON ( WarehouseSubType.warehouseTypeId = WarehouseType.id)
INNER JOIN WarehouseSubType1
ON (WarehouseSubType.id = WarehouseSubType1.warehouseSubTypeId)) a
WHERE warehouseTypename like '%Ve%' ) b
WHERE b.row > 0 and b.row< 75

how can I tell if the last x rows of 'state' = 1

I need help with a SQL query.
I have a table with a 'state' column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of 'state' = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here's you could probably do it :
SELECT IF(SUM(x.state) = 5, 1, 0) AS is_consecutive
FROM (
SELECT state
FROM table
WHERE Processor = 3
ORDER BY Status_datetime DESC
LIMIT 5
) as x
If is_consecutive = 1, then, yes, there is 5 last consecutive rows with state = 1.
Edit : As suggested in the comments, you'll have to use ORDER BY in your query, to get the last nth rows.
And for more accuracy, since you have a timestamp column, you should use Status_datetime to order the rows.
You should be able to use something like this (replace the number in the HAVING with the value of x you want to check for):
SELECT Processor, OpenCount FROM
(
SELECT TOP 10 Processor, DateTime, Sum(Status) AS OpenCount
FROM YourTable
WHERE Processor = 3
ORDER BY DateTime DESC
) HAVING OpenCount >= 10