PHP/SQL: Prevent Option values from repeating - php

So I am trying to create a query that shows the list of all the dates customers came in to order food. However, since there were multiple customers for a particular day and thats how the values have been stored in the table, whenever I turn the following query:-
SELECT DateOfEntry FROM KFC;
Although it works and I see all the details, I dont want the same date value to repeat. For example if Two customers A and B, came in on 11/11/17 to order food, I would get the result of the above mentioned query with two records of same date. I dont want the same dates to repeat. What do I do?
I know DISTINCT works but the problem is, I am creating a php page where admins can check the list of all the users who came in on a particular day. So I am using the select and option methods of HTML to select a date which then shows the list of the users who made an entry that day. With this code, only one record is shown

Try:
SELECT DISTINCT DateOfEntry FROM KFC;

Try :
SELECT DISTINCT DateOfEntry FROM KFC;
or this
SELECT DateOfEntry FROM KFC
GROUP BY DateOfEntry;

Related

MySQL Group Identical titles (row) and sum the money(row) but keep individual timestamp for sorting

I have been all over the internet to find answer for this, but I can't find any. (I might be asking incorrectly?)
I have a table which looks like this:
I have been using the query:
SELECT title, date, SUM(money)
FROM payments
WHERE username = '$username_n' and type=0
GROUP BY title
It does the trick, and it groups the identical titles and sum up their values
But as you can see, it only gives me ONE date, and I would like to be able to make a slider, where I can sort the date from the date, but I can't do that if it creates only one date for each grouped title.
How can I create multiple dates, but still get rid of the duplicated titles?
Try this:
SELECT title, date, SUM(money) FROM payments
WHERE username = '$username_n' and type=0 GROUP BY title, date(`date`);
It is a little weird with fields in the field list that are not aggregated or part of the grouping. As you had before with the date field:
SELECT title, date, SUM(money) FROM payments
WHERE username = '$username_n' and type=0
GROUP BY title
You group by title, and aggregate the money field, all good. The date field is not part of grouping or aggregation. What mysql is doing, it will just take the field value for the date field in the aggregation. Thats why you only see one date value in your result. As code-monk explained you need to use date field in the grouping. In MS SQL Server your query would actually error our, as MS SQL Server doesnt allow fields that are not part of grouping or aggregated, which I think is very good.
Below query will give date values in comma separated manner. You can use PHP/server side code to explode those by comma and create a list/array and return it to client side i.e. JavaScript. Use javascript to sort the list.
SELECT title, group_concat(date) as `dates`, SUM(money)
FROM payments
WHERE username = '$username_n' and type=0
GROUP BY title

MySQL Query showing only the last item grouped by user

I got a database that registers user actions and their geolocation.
Now I would like to fetch this data at the hand of the last action per user.
The table looks a bit like:
geoaction_id AUTO INCREMENT
geoaction_user
geoaction_creationdate (Y-m-d H:i:s)
geoaction_action
geoaction_lon
geoaction_lat
Now I would like to make a simple query that selects of all users the last item.
But LIMIT 0,1 just parses one row no matter what. (LOGICALLY!!)
Group by gives a little better result.
But how to get only the last item per user?
Try this, please provide the queries you have checked out so far, in order to assist you better.
SELECT geoaction_user, geoaction_action
FROM table-name
GROUP BY geoaction_user
ORDER BY geoaction_action DESC LIMIT 1
Working with sets:
SELECT
g.geoaction_user,
g.geoaction_action,
g.geoaction_creationdate,
g.geoaction_lat,
g.geoaction_lon
FROM
(
SELECT
geoaction_user,
MAX(geoaction_id) max_id
FROM
geoactions
GROUP BY geoaction_user
) s
JOIN
geoactions g
ON s.geoaction_user = g.geoaction_user
AND s.max_id = geoaction_id
The subquery generates a virtual table with the geoaction_id from the latest entry in the tabble for each user_id, then the table is joined to get the data belong to the latest id.
If you need to filter out some records place the where clause in the subquery

mysql group by any one or more of several fields

I am looking to group by the following criteria:
so lets say I have how can I group them all
product1,brand1,null,null,12,null,1234
product2,brand1,null,null,12,null,null
product3,brand2,null,null,null,1234
product1,brand1,null,null,null,null,null
(product_name AND brand_name) upc or isbn or mpn or ean or model_number
is it possible to create a single select statement to return these grouped values ?
Many thanks in advance
The short answer is "No." How would MySQL know which one you want to group by in a particular situation? It wouldn't. You need to send MySQL a different SQL query for each situation.
If in your UI the user chose to group by one thing, send MySQL a query to group by that. If they chose to group by something else, send MySQL a query to group by that. You have to choose in PHP before you tell MySQL what to do. You can't expect MySQL to read your mind and know "in this case he wants to group by X."
You can, however, group by all those fields, giving them a priority. I.e. group by this one first, then by that one, etc. Like:
SELECT * from products
group by
product_name, brand_name,
upc, isbn, mpn, ean, model_number
But this is not going to group by one or the other. It will group by all of them, giving first priority to the first mentioned.

SELECT DISTINCT still showing duplicates

Yes, there's a thousand questions about this on SO, but I've been searching for half an hour and I've yet to find a solution.
So, I've a table like this:
And this is my query:
SELECT DISTINCT rengasID,leveys FROM renkaat ORDER BY leveys ASC
And this is the result I get:
If you get the idea, I'm populating a select field with it, but it still has duplicates.
What am I doing wrong?
If you want distinct leveys, just choose that field:
SELECT DISTINCT leveys
FROM renkaat
ORDER BY leveys ASC
The rengasid has a different value on each row.
The distinct clause applies to all the columns being returned, regardless of parentheses.
EDIT:
If you need the regasid in the result, then use group by:
select leveys, min(regasid) as regasid
from renkaat
group by leveys
order by leveys asc;
This gives the first id. If you need all of them, you can get them in a list using group_concat(). If you need a separate id on each row, well, then you have duplicates.
Your rengasID is still different in each shown line. The distinct will check a mix of every selected field, so in this case it will search a distinct combination of rengasID and leveys.
You cannot ask for your ID here, since MySQL has no way of knowing which one you want.
Depending on what you want to do it can be more correct to save your "leveys" (I'm not sure what they are) in a separate table with a unique ID and join it. For filling up your list with all possible leveys, you can just query that new table.
This can be important because using group by, you can get random results for id's later on.
This is because you are selecting combination of rengasID and leveys. And what you are getting as a result is a distinct combination of the two.
To achieve what you are trying, see the answer of #GordonLinoff.

php/mysql add rows together to get total

here's the scenario. I am generating a report of all the members who have dues to pay for a certain time period.
I am successfully selecting and displaying each database entry as a row in a html table.
The problem is the total fields the report must have. Each member pays different amounts based on what services they use, so I must add the values in each field individually to ensure proper result.
Question is, how do I go about adding the rows/field together?
Edit:
To clarify. I am adding dues paid and donations paid fields. They are classified and integer in mysql database.
Example, let's say that my query returns 3 results. I wish to add the dues paid for all 3 results and display it as total_dues_paid. Same idea for donations.
This must be dynamic for any given number of results, as this changes month to month and we have seen several hundred results in some months.
Thanks
To add fields (columns):
SELECT col1, col2, col3, (col1+col2+col3) AS Total FROM table;
To add rows together, use the SUM() aggregate:
SELECT
userid,
SUM(col1) AS col1_total,
SUM(col2) AS col2_total
FROM table
GROUP BY userid
You can add in your query string.
SELECT (field1 + field2) AS Total
FROM table

Categories