MySQL Query with DISTINCT and ORDER BY - php

Here is my query:
$result = $mysqli->query('SELECT DISTINCT SKU_SIZE_PART1
FROM SKU_DATA
ORDER BY SKU_SIZE_PART1 DESC');
Now this works perfect for SKU_SIZE_PART1 but I have 2 more parts that I need to grab. Now when I put a comma and do this: 'SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3' then the DISTINCT doesn't work and I get a ton of duplicates, and then I'm not sure how to order the query so that all of them are ordered by the size and DESC.
Does that make sense? I could just duplicate that query 2 more times and have 3 separate queries but I would like to know how to accomplish this with just one.

I'm not positive that I understand what you're trying to do, but it sounds like you might actually want something like this:
SELECT SKU_SIZE_PART1 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS SKU_SIZE_PART
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS SKU_SIZE_PART
FROM SKU_DATA
ORDER BY SKU_SIZE_PART DESC
which will return all distinct SKU_SIZE_PART1/2/3 values in a single column, rather than all distinct (SKU_SIZE_PART1, SKU_SIZE_PART2, SKU_SIZE_PART3) triads in three columns.

After reading your question several times, I figured this might be what you are looking for:
SELECT SKU_SIZE_PART1 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART2 AS ssp
FROM SKU_DATA
UNION
SELECT SKU_SIZE_PART3 AS ssp
FROM SKU_DATA
ORDER BY ssp DESC

SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1)
ORDER BY d.sku_size_part1 DESC
Note that this will select rows more or less at random.
Although all sku_size_parts will be from the same row, lots of values will be hidden.
If you want to make the query stable, you need to add a having clause in the inner subselect.
Something like this:
SELECT d.sku_size_part1, d.sku_size_part2, d.sku_size_part3
FROM sku_data d
WHERE d.id IN (
SELECT s.id <<--- replace `id` with the real primary-key for table `sku_data`
FROM sku_data s
GROUP BY s.sku_size_part1
HAVING s.sku_size_part2 = MIN(s.sku_size_part2)
AND s.sku_size_part3 = MIN(s.sku_size_part3))
ORDER BY d.sku_size_part1 DESC
Either that or you want #bfavaretto's UNION variant.

DISTINCT selects a distinct set of rows, not columns... the assumption/problem here is how to condense multiple columns. If you had the following table
sku1 | sku2 | sku3
---------------------
a | a | b
b | b | b
Telling it to select destinct would return both rows because none of them are distinct, you couldn't just remove the third column because then the row data would be inconsistent. If you want everything in one table you can do this with subqueries.
SELECT (SELECT DISTINCT SKU_SIZE_PART1 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part1, (SELECT DISTINCT SKU_SIZE_PART2 FROM SKU_DATA ORDER BY SKU_SIZE_PART2 DESC)
as part2, (SELECT DISTINCT SKU_SIZE_PART3 FROM SKU_DATA ORDER BY SKU_SIZE_PART1 DESC)
as part3 FROM SKU_DATA
You can read up a little on how DISTINCT works to see why you can't just do SELECT DISTINCT SKU_SIZE_PART1, PART2, PART3. Somewhere like This Link

Related

Mysql Query search by latest date with group by

This is my table structure
and this is my dataset
What I want is query that gets data ordered by date desc and group by id_patient
so the result in the dataset example should be like this:
I would go with limit clause with subquery since you have PK :
select *
from table t
where id = (select t1.id
from table t1
where t1.id_patient = t.id_patient
order by t1.date desc
limit 1
);
However, if single patient has multiple same dates then this would produce only single records based on date.
SELECT * from rdv a JOIN (SELECT id_patient,MAX(date) date FROM rdv GROUP by id_patient ) b on a.id_patient = b.id_patient and a.date = b.date
If you want the latest record for each patient, then you are not looking for an aggregation. I would often approach this with a correlated subquery:
select t.*
from t
where t.date = (select max(t2.date) from t t2 where t2.id_patient = t.id_patient);
SELECT *
FROM table
GROUP BY group by id_patient
ordered by DATE(date) desc;

SQL: How can I find repeated value's till separation

What I want to do with SQL is find repeating values in a column until there's an other value sorted by date.
This is my table. I use "CURRENT_TIMESTAMP" for the date column.
Name |date
-------------------
Bart |12-12-2014
Bart |23-12-2014
Joost |24-12-2014
Bart |25-12-2014
Bart |26-12-2014
Bart |27-12-2014
So in this example I want the number "3" returned and the last known value of name, so in this case "Bart".
I hope I made myself clear, sorry for the unclear title!
Hmmm . . . Here is one method where the logic is placed in the where clause:
select count(*), max(lastname.name)
from tablename t cross join
(select t2.name from tablename t2 order by date desc limit 1) as lastname
where t.name = lastname.name and
t.date > (select max(t2.date) from tablename t2 where t2.name <> lastname.name);
You can use the following query:
SELECT Name, COUNT(*) AS cnt
FROM (
SELECT Name, [date],
ROW_NUMBER() OVER (ORDER BY [date]) -
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY [date]) AS grp
FROM mytable ) AS t
GROUP BY Name, grp
ORDER BY COUNT(*) DESC
This query tries to identify islands of records, i.e. consecutive rows having the same Name: grp calculated field does exactly this.
If you want the Name having the largest number of consecutive records, just use TOP 1 in the above query.
Demo here

Mysql group_concat with distinct and where gives strange results

I have the following query which works fine (see below).
But when I add a condition, for example AND (specialtyName = '...') the main results are fine, but the GROUP_CONCAT only shows the results that match the condition.
Can anyone please help me with this?
Thanks in advance.
Fred.
SELECT
tblJobs.jobID,
tblJobs.jobName,
DATE_FORMAT(tblJobs.jobDate,'%d-%m-%Y'),
tblCompanies.companyID,
tblCompanies.companyName,
tblCompanies.companyNameConvert,
GROUP_CONCAT(DISTINCT tblSpecialties.specialtyName
ORDER BY FIELD (
specialtyName,
'specialtyName1',
'specialtyName2',
'specialtyName3'),
specialtyName ASC)
AS specialtyNames,
GROUP_CONCAT(DISTINCT tblSpecialties.specialtyNameConvert
ORDER BY FIELD (
specialtyName,
'specialtyName1',
'specialtyName2',
'specialtyName3'),
specialtyName ASC)
AS specialtyNamesConvert,
GROUP_CONCAT(DISTINCT tblRegions.regionName),
GROUP_CONCAT(DISTINCT tblRegions.regionNameConvert)
FROM tblJobs
LEFT JOIN tblCompanies ON
(tblJobs.jobCompany = tblCompanies.companyID)
LEFT JOIN tblSpecialties ON
FIND_IN_SET(tblSpecialties.specialtyID, REPLACE(tblJobs.jobSpecialty,' ',','))
LEFT JOIN tblRegions ON
FIND_IN_SET(tblRegions.regionID, REPLACE(tblJobs.jobRegion,' ',','))
WHERE
AND jobActive = '1'
AND jobDate >= '2013-01-01'
AND companyActive = '1'
GROUP BY jobID
ORDER BY jobDate DESC, jobID DESC, jobCompany DESC
If you say:
WHERE jobActive = '1' AND jobDate >= '2013-01-01' AND companyActive = '1' AND
specialties = XXX
Then you are only going to get exactly those specialties. The filtering is done before the aggregation. As a note: including such conditions in the where clause also turns the outer joins to inner joins. Your joins are probably on properly aligned foreign key relationships, so inner joins may be appropriate.
I'm guessing what you really want is to filter jobs by those having that specialty, but to keep all other information. You want to do the filtering after the aggregation. Do this with a having clause instead of a where clause:
having sum(specialties = XXX) > 0;
This will keep only the rows that have the particular specialty, and keep all the other information.
I suppose that using aliases for your tables and subqueries could resolve your problem.
You can try something like this:
SELECT
tblJobs.jobID,
tblJobs.jobName,
DATE_FORMAT(tblJobs.jobDate,'%d-%m-%Y'),
tblCompanies.companyID,
tblCompanies.companyName,
tblCompanies.companyNameConvert,
(SELECT GROUP_CONCAT(DISTINCT ts.specialtyName
ORDER BY FIELD (
specialtyName,
'specialtyName1',
'specialtyName2',
'specialtyName3'),
specialtyName ASC)
FROM tblSpecialties ts) AS specialtyNames ,
, ... ,
FROM tblJobs
LEFT JOIN tblCompanies ON
(tblJobs.jobCompany = tblCompanies.companyID)
LEFT JOIN tblSpecialties ON
FIND_IN_SET(tblSpecialties.specialtyID, REPLACE(tblJobs.jobSpecialty,' ',','))
LEFT JOIN tblRegions ON
FIND_IN_SET(tblRegions.regionID, REPLACE(tblJobs.jobRegion,' ',','))
WHERE
AND jobActive = '1'
AND jobDate >= '2013-01-01'
AND companyActive = '1'
GROUP BY jobID
ORDER BY jobDate DESC, jobID DESC, jobCompany DESC
I didn't tested this code, but It could help.

PHP / MYSQL Statement so select Max Sum of Join

I have Problems with a select statement, as a little help here are the important columns:
Table1
ID NAME
TABLE 2
ID U_ID COUNTER
The ID of Table 1 Matches the U_ID of Table 2. Table 2 contains many entries for the same u_id.
What I want to do is to get the Name of the "user" (table 1) who has in sum the max. counter.
What I got since now is the join of the tables (Where clause depends on other rows which are not important for the problem).
Can anyone help me on this issue?
So what you need is an aggregate of an aggregate (max of sum of column). The easiest will be to create a view providing the sum and u_id end then select the max of it:
create view table2sums
as
select u_id, sum(counter) as total
from table2
group by u_id;
and then
select t1.name
from table1 t1, table2sums t2
where t1.id = t2.u_id
and t2.total >= all (
select total
from table2sums
)
In this special case you can also do it directly:
select t1.name
from table1 t1, table2 t2
where t1.id = t2.u_id
group by t1.name
having sum(t2.counter) >= all (
select sum(counter)
from table2
group by t2.u_id
)
NOTE: The other proposed solutions will show a better performance. My solution only selects the name (which is what you said you wanted) and works in any RDBMS.
There exist RDBMS without the LIMIT possibility.
In the end, I'd say: regard my solution as educational, the others as practical
SELECT name,
SUM(counter) as counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY counter DESC
LIMIT 1
You can try this:
SELECT name, SUM(counter) as total_counter
FROM table1
JOIN table2
ON table1.id = table2.u_id
GROUP BY u_id
ORDER BY total_counter DESC
LIMIT 1
Working Demo: http://sqlfiddle.com/#!2/45419/4

how to order resultset based on fields from two tables

I have two tables one for topic_likes & one for user_comments.I must get recent updates of like & comment from this tables.Given below is the sql :-
SELECT (required fields...)
LEFT JOIN topic_likes AS TL ON (TL.delete_status=0 AND TL.user_id!=$user_id)
LEFT JOIN user_comments AS UC ON (UC.delete_status=0 AND UC.user_id!=$user_id)
WHERE
(TL.created_date >= '$lastLogin' OR UC.created_date >= '$lastLogin'
ORDER BY UC.created_date desc,TL.created_date desc
LIMIT $limit
I have given order by two fields from two tables(UC.created_date, TL.created_date)
But it does not order the resultset based on created_date from topic_likes.It only orders the results based on user_comments table
But if I removed the limit condition it gives correct results...!!
Any suggestion appreciated
This is a strange approach you're taking. If you want to display user's likes and comments using a single query you should UNION the results. Example:
SELECT * FROM
(
SELECT id, `date`, 'like' as `type` FROM topic_likes
UNION
SELECT id, `date`, 'comment' as `type` FROM user_comments
) a order by a.date DESC limit 5;
The result should be similar to this:
But there are limitations. The number of columns from each subquery must match.

Categories