mysql GROUP_CONCAT DISTINCT multiple columns - mysql

I have a tag field for a blog posts. tags have unique id but their displayName might be duplicated. What I want is a query that selects posts and in all_tags field we get couples of (id,displayName) is this way:
id1,name1;id2,name2;id3,name3
My query looks like:
select ....
CONCAT_WS(';', DISTINCT (CONCAT_WS(',',tags.id,tags.displayName))) AS all_tags
Join ...post content ...
Join ...post_tags ...
Join ...tags ...
ORDER BY posts.id
This line causes problem:
CONCAT_WS(';', DISTINCT (CONCAT_WS(',',tags.id,tags.displayName))) AS all_tags
How should I modify it?
Some people use an inner (SELECT .. FROM) but as I have heard, it is so inefficien
SELECT `posts`.*,`categories`.*,`creators`.*,`editors`.*
CONCAT_WS(';', DISTINCT GROUP_CONCAT(CONCAT_WS(',',tags.id,tags.displayName))) AS all_ids
FROM (`posts`)
LEFT JOIN `languages` ON `posts`.`language_id`=`languages`.`id`
LEFT JOIN `users` as creators ON `posts`.`creatorUser_id`=`creators`.`id`
LEFT JOIN `users` as editors ON `posts`.`lastEditorUser_id`=`editors`.`id`
LEFT JOIN `userProfiles` as editors_profile ON `editors`.`profile_id`=`editors_profile`.`id`
LEFT JOIN `categories` ON `posts`.`category_id`=`categories`.`id`
LEFT JOIN `postTags` ON `postTags`.`post_id`=`posts`.`id`
LEFT JOIN `tags` ON `postTags`.`tag_id`=`tags`.`id`
LEFT JOIN `postTags` as `nodetag_checks` ON `nodetag_checks`.`post_id`=`posts`.`id`
LEFT JOIN `tags` as `tag_checks` ON `nodetag_checks`.`tag_id`=`tag_checks`.`id`
WHERE ( 9 IN(`tag_checks`.`id`,`tag_checks`.`cached_parents`) OR 10 IN(`tag_checks`.`id`,`tag_checks`.`cached_parents`) OR 11 IN(`tag_checks`.`id`,`tag_checks`.`cached_parents`))
GROUP BY `posts`.`id` ORDER BY `posts`.`created` desc LIMIT 0, 20

Try this:
GROUP_CONCAT(
DISTINCT CONCAT(tags.id,',',tags.displayName)
ORDER BY posts.id
SEPARATOR ';'
)

As advised by #Willa, I add my comment as an anwser :
GROUP_CONCAT allows you to concat multiple fields :
GROUP_CONCAT(tags.id, ',', tags.displayName)
The only difference with Stephan's answer is in case your code allows the same tag to be affected several times to one post OR if you JOIN sequence leads you to multiselect the same record in the tag table. In those case, my solution will return the same tags multiple times.

On top of #Stephan's great answer, to prevent the same content showing up multiple times due to multiple JOIN's in your query but you don't want the id to show in the output...
GROUP_CONCAT(
DISTINCT
tags.displayName,
'||__', tags.id, '__||'
SEPARATOR '\n'
)
And then loop over the result in the end removing everything between ||__ and __|| .
This example is for php:
$data = preg_replace("/\|\|__.*__\|\|/", '', $data);

Related

How can I use SQL_CALC_FOUND_ROWS when having multiple selects

I'm working on a pagination code that uses SQL_CALC_FOUND_ROWS() but when I limit the results per page to a number less than the total results, "SELECT FOUND_ROWS() as total" will return the number of elements per page and not the total of found rows. Since I'm doing a select from the second select, this result makes total sense but I don't know how to solve this. How can I pass the total results from inside to the external select?
My code(please, ignore quotation marks for php escaping):
SELECT SQL_CALC_FOUND_ROWS userid,contaTipo,userNome,nomeFantasia,sexo,cidade,estado,bairro,imovelN,logradouro,avaliacao,imagem,formasPagamento,estabelecimento,profissao
FROM
(SELECT vw_Busca.userid as userid, vw_Busca.contaTipo as contaTipo, vw_Busca.userNome as userNome,
vw_Busca.nomeFantasia as nomeFantasia,vw_Busca.sexo as sexo, vw_Busca.cidade as cidade, vw_Busca.estado as estado, vw_Busca.bairro as bairro,
vw_Busca.imovelN as imovelN,vw_Busca.logradouro as logradouro,tipoProfissionalPF.tipo as profissao, tipoProfissionalPJ.tipo as estabelecimento,
vw_userRating.total as avaliacao, GROUP_CONCAT(especialidades.especialidade SEPARATOR ', ') as especs,
vw_Busca.imagem as imagem, GROUP_CONCAT( DISTINCT userPagamento.formaPagamento SEPARATOR ', ') as formasPagamento
FROM vw_Busca
LEFT JOIN usersEspec ON usersEspec.userid=vw_Busca.userid
LEFT JOIN especialidades ON especialidades.id=usersEspec.especialidade
LEFT JOIN userPagamento ON userPagamento.userid=vw_Busca.userid
LEFT JOIN profissionais ON profissionais.userid=vw_Busca.userid
LEFT JOIN tipoProfissionalPF ON tipoProfissionalPF.id=profissionais.profissao
LEFT JOIN empresaDados ON empresaDados.userid=vw_Busca.userid
LEFT JOIN tipoProfissionalPJ ON tipoProfissionalPJ.id=empresaDados.tipoProfissionalPJ
LEFT JOIN vw_userRating ON vw_userRating.userid=vw_Busca.userid
WHERE vw_Busca.cidadeId='$cidade' AND (vw_Busca.userNome LIKE '%".$termo."%' OR
vw_Busca.nomeFantasia LIKE '%".$termo."%'
OR vw_Busca.tags LIKE '%".$termo."%')
GROUP BY userid LIMIT $inicio,$qtd) as mainTable
ORDER BY mainTable.avaliacao DESC
It appears that your outer select is just ordering results returned from the inner query. Since your inner query has already applied a LIMIT, your outer query is actually performing sort on partial results, which seems incorrect to me. So I guess you can remove the outer select altogether which would solve your problem.
By the way, just noticed that the SQL_CALC_FOUND_ROWS query modifier and accompanying FOUND_ROWS() function are deprecated as of MySQL 8.0.17 as per the documentation

How make a subquery in a select all (*) for a view SQL?

I want to make a view with multiple tables, but what I need is select * from a table and select just a few from the rest of the tables. This is what I have until now:
CREATE VIEW `database`.`pages_view` AS
SELECT
`p`.`p_name` AS `p_name`,
`p`.`slug` AS `slug`,
`i`.`image` AS `image`,
`t`.`title` AS `title`,
`t`.`text` AS `text`,
`s`.`sec_name` AS `sec_name`
FROM
(((`database`.`pages` `p`
LEFT JOIN `database`.`page_image` `i` ON ((`p`.`id` = `i`.`pages_id`)))
LEFT JOIN `database`.`page_text` `t` ON ((`p`.`id` = `t`.`pages_id`)))
LEFT JOIN `database`.`sections` `s` ON ((`p`.`id` = `t`.`pages_id`)))
WHERE
(`p`.`visible` = 1)
What I want is select * from sections instead of call one by one.
Just use s.*:
CREATE VIEW database.pages_view AS
SELECT p.p_name, p.slug, i.image, t.title, t.text,
s.*
FROM database.pages p LEFT JOIN
database.page_image i
ON p.id = i.ages_id LEFT JOIN
database.page_text t
ON p.id = t.pages_id LEFT JOIN
database.sections s
ON p.id = t.pages_id
WHERE p.visible = 1;
Notes:
You do not need escape characters (unless you have poorly named table or columns).
This is especially true for table aliases.
You don't need to rename columns to the same name p.p_name as p_name is redundant -- not worth the extra typing.
Also, be careful when using * in a view. It is the answer to your question, but the names, types, and order of the columns depends on the underlying table.

MySQL Query show results based on multiple filters/tags

This has been asked in different ways before, but I can't seem to get something that works for what I need exactly.
The goal here is to make a search query that returns Photos based on tags that are selected. Many tags can be applied to the filter simultaneously, which would need to make it so that the query only returns photos that have ALL of the tags selected. Think of any major web shop where you are narrowing down results after performing a basic keyword search.
Table1: Photos
ID|Title|Description|URL|Created
Table2: PhotosTagsXref
ID|PhotoId|TagId
Table3: PhotosTags
ID|Title|Category
What I have:
SELECT p.* FROM `PhotosTagsXref` AS pt
LEFT JOIN `Photos` AS p ON p.`ID` = pt.`PhotoId`
LEFT JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
WHERE p.`Description` LIKE "%test%" AND
????
GROUP BY p.`ID`
ORDER BY p.`Created` DESC LIMIT 20
The ???? is where I've tried a bunch of things, but stumped. Problem is I can easily find a result set that contains photos with one tag or another, but if applying 2, 3, or 4 tags we'd need to only return photos that have entries for all of those tags in the database. I think this will involve combining result sets but not 100% sure.
Example:
Photo 1 Tags: Blue, White, Red
Photo 2 Tags: Blue
Searching for a photo with tags of 'blue' returns both photos, searching for a photo with tags of 'blue' and 'white' returns only Photo 1.
Supposing the requested set of tags is (red,blue) you can do:
SELECT * FROM `Photos`
WHERE `Description` LIKE "%test%"
AND `ID` IN (
SELECT pt.`PhotoId` FROM `PhotosTagsXref` AS pt
JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
WHERE t.Title in ('red','blue') /* your set here */
GROUP BY pt.`PhotoId` HAVING COUNT(DISTINCT t.`TagId`)=2 /* # of tags */
)
ORDER BY `Created` DESC LIMIT 20
Apparently, the tag set needs to be created dynamically, as well as its count.
Note: I'm counting DISTINCT TagIDs because I don't know your table's constraints. If PhotosTagsXRef had a PK/UNIQUE (PhotoId,TagId) and PhotosTags had a PK/UNIQUE (TagId), then COUNT(*) would suffice.
Admittedly a bit ugly. But assuming that PhotosTags.Category has the 'Blue', 'White', etc, try something along this line.
SELECT p.*
From `Photos` AS p
WHERE p.`Description` LIKE "%test%" AND
AND Exists
( Select 1 FROM `PhotosTagsXref` AS pt
Inner JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
Where pt.`PhotoId` = p.`ID`
And t.Category = 'FirstCatToSearch'
)
AND Exists
( Select 1 FROM `PhotosTagsXref` AS pt
Inner JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
Where pt.`PhotoId` = p.`ID`
And t.Category = 'SecondCatToSearch'
)
AND Exists
( ...
)
...
SELECT p.* FROM `PhotosTagsXref` AS pt
LEFT JOIN `Photos` AS p ON p.`ID` = pt.`PhotoId`
LEFT JOIN `PhotosTags` AS t ON pt.`TagId` = t.`ID`
inner join (select PhotoId from PhotosTagsXref
LEFT JOIN `PhotosTags` AS t
ON pt.`TagId` = t.`ID`
where (t.title = 'cond 1' or t.title = 'cond 2' ...)
--where t.title in (list condition) **this works as well**
having count(1) = (count of conditions) ) filter
on filter.photoID = pt.PhotoID
WHERE p.`Description` LIKE "%test%"
GROUP BY p.`ID`
ORDER BY p.`Created` DESC LIMIT 20
That should work, I made some assumptions on what column to use for the filter and joins, you may need to retool...the inner join functions as a filter and should pull out only records that have the number of matches equal to the total of the number of matches submitted. Now you just need a language to plug in those conditions and condition count values.

Select two table and order by date

i am trying to list out the latest update from two table doc_to_do and doc_bug_tracker below is my table structure
doc_to_do
doc_bug_tracker
and this is my current query :
$sth = $this->db->prepare('SELECT p.*,
dtd.projects_id as dtd_projects_id, dtd.content as dtd_content, dtd.date_modified as dtd_date_modified,
dbt.projects_id as dbt_projects_id, dbt.content as dbt_content, dbt.date_modified as dbt_date_modified
FROM `projects` p LEFT JOIN `doc_to_do` dtd ON p.id=dtd.projects_id
LEFT JOIN `doc_bug_tracker` dbt ON p.id=dbt.projects_id
where p.id="'.$project_id.'"');
so now how to order by date_modified either from table doc_to_do or doc_bug_tracker ?
To get only the latest date (as opposed to all dates, ordered descending) try this:
SELECT
p.id,
MAX(GREATEST(dtd.date_modified, dbt.date_modified)) AS MaxDate
FROM projects p
LEFT JOIN doc_to_do dtd ON p.id = dtd.projects_id
LEFT JOIN doc_bug_tracker dbt ON p.id = dbt.projects_id
WHERE p.id = <project_id>
GROUP BY p.id
If you need additional columns in your SELECT, be sure to include them in your GROUP BY.
This should work :
'SELECT x.* FROM
(
SELECT p.*,
dtd.projects_id as dtd_projects_id, dtd.content as dtd_content, dtd.date_modified as dtd_date_modified,
dbt.projects_id as dbt_projects_id, dbt.content as dbt_content, dbt.date_modified as dbt_date_modified
FROM `projects` p LEFT JOIN `doc_to_do` dtd ON p.id=dtd.projects_id
LEFT JOIN `doc_bug_tracker` dbt ON p.id=dbt.projects_id
where p.id="'.$project_id.'"
) x
ORDER BY x.date_modified ASC'

Count(*) won't display when result is 0 when using group by

Sorry I couldn't think of a better title...
So I have this database where I have a table 'bibitems' (basicly texts) 'tags' (the tags the text is filed under) and 'taglinks' (Associative entity between a bibitem and a tag)
So on my page I want to display every tag, directly followed by the amount of documents that are filed under that tag
so if 2 documents are under 'java' it's say
java (2)
SELECT
tags.id,
naamnl,
COUNT(*) AS 'count'
FROM
tags,
bibitems,
taglinks
WHERE
bibitems.id=taglinks.item_id
AND tag_id=tags.id
GROUP BY
naamnl
This works well when there are documents filed under a certain tag.
But when there's a tag that's unused so far, it won't pop up.
I want it to display java (0) in that case
SELECT tags.id,
tags.naamnl,
COUNT(bibitems.id) AS 'count'
FROM tags
LEFT
OUTER
JOIN taglinks
ON taglinks.tag_id = tags.id
LEFT
OUTER
JOIN bibitems
ON bibitems.id = taglinks.item_id
GROUP
BY tags.id,
tags.naamnl
;
or
SELECT tags.id,
tags.naamnl,
( SELECT COUNT(*)
FROM bibitems
WHERE bibitems.id IN
( SELECT taglinks.item_id
FROM taglinks
WHERE taglinks.tag_id = tags.id
)
)
FROM tags
GROUP
BY tags.id,
tags.naamnl
;
(Take your pick.)
Read on the difference between INNER and OUTER joins.
SELECT tag.id, naamnl, COUNT(taglink.tag_id) AS 'count'
FROM tag
LEFT OUTER JOIN taglink ON taglink.tag_id=tag.id
LEFT OUTER JOIN bibitem ON bibitem.id=taglink.item_id
GROUP BY tag.id, naamnl
Something like thisshould do it. (It is difficult since you did not provide the full table schemas.)
SELECT
i.naamnl,
COALESCE(t.Count, 0) AS Count
FROM
bibitems i
LEFT JOIN
(
SELECT
tag_id,
COUNT(*) AS Count
FROM
taglinks
GROUP BY
item_id
) t
ON
t.tag_id = i.tag_id