Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DISTINCT does not work for MSSQL databases #297

Open
Lokilein opened this issue Dec 4, 2023 · 0 comments
Open

DISTINCT does not work for MSSQL databases #297

Lokilein opened this issue Dec 4, 2023 · 0 comments
Labels
Bug Something isn't working

Comments

@Lokilein
Copy link

Lokilein commented Dec 4, 2023

Bug Report

Q A
Version(s) 2.18.0

Summary

Selecting data by the Select-Class and AbstractTableGateway::selectWith returns data like you didn't use DISTINCT.

Current behavior

I have a query which performs differently on MySQL and MSSQL. When executing the query on MySQL, everything works as expected, but for MSSQL, it behaves as if I didn't enter the DISTINCT keyword.

How to reproduce

Here is my Select I'm building, with the input parameter $userid


$select = new Select(SurveySet::TABLE_NAME);
$select->quantifier(Select::QUANTIFIER_DISTINCT); // <-- DISTINCT
$select->columns(['id', 'name']);
$select->join(['s'   => Survey::TABLE_NAME], 's.survey_set_id = id', []);
$select->join(['c'   => Course::TABLE_NAME], 'c.verid = s.verid', []);
$select->join(['sta' => SubunitToAdministrate::TABLE_NAME], 'sta.subunitid = c.subunitid', []);
$select->where->equalTo('sta.userid', $userid);

$result = $this->surveySetTable->selectWith($select);

When I output the query with $select->getSqlString($this->surveySetTable->getAdapter()->getPlatform()), this looks fine. The query also works correctly on both SQL versions when I copy it:
For MSSQL this is:

SELECT DISTINCT [survey_set].[id] AS [id], [survey_set].[name] AS [name] 
FROM [survey_set] 
INNER JOIN [survey] AS [s] ON [s].[survey_set_id] = [id] 
INNER JOIN [course] AS [c] ON [c].[courseid] = [s].[courseid] 
INNER JOIN [subunit_to_admin] AS [sta] ON [sta].[subunitid] = [c].[subunitid] 
WHERE [sta].[userid] = '210'

And for MySQL it is:

SELECT DISTINCT `survey_set`.`id` AS `id`, `survey_set`.`name` AS `name` 
FROM `survey_set` 
INNER JOIN `survey` AS `s` ON `s`.`survey_set_id` = `id` 
INNER JOIN `course` AS `c` ON `c`.`courseid` = `s`.`courseid` 
INNER JOIN `subunit_to_admin` AS `sta` ON `sta`.`subunitid` = `c`.`subunitid` 
WHERE `sta`.`userid` = '210'

The relation survey_set to survey is 0..1 to 1..n. So a survey_set can be in multiple survey entries. Some surveys can be in no set (they are null then). The other joins might not be relevant, I guess any 1..n relation would bring a similar result.
For MSSQL, I have 9 sets in 69 surveys and I get 69 results instead of 9, which I get when I execeute the query manually in MSSQL.
For MySQL, I got 23 sets in 189 surveys and I got 23 results, which is the same as the SQL query gives me.

Based on the SQL-String, you can also tell that the select does consider and receive this value, but it seems to be ignored at execution. This also make me think that I am not using it wrong.

Expected behavior

I want the result of "selectWith($select)" to be the same as the DB-query that the $select-Query returns.

@Lokilein Lokilein added the Bug Something isn't working label Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant