How to Make two or more Select categorized in the same query

Asked

Viewed 158 times

1

I need to take all words with 'A' and play 'Palavrascoma' and all words with 'B' and play Palavrascomb, and can have C, D E... The closest so far was that, but only with the first.

SELECT
    w1.wd as PalavrasComA,
    w2.wd as PalavrasComB
From ptbr as w1, ptbr as w2 

where left (w1.wd, 1) = 'a'

and left (w2.wd, 1) = 'b';
  • It will be variable or you know all combinations?

  • It will be variable because I will type words in a search field and will return by the number of words that starts with each letter.

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

1 answer

0

If you want to bring them all in one query fixed can do as in the example:

SELECT COUNT(CASE WHEN left(w.wd, 1) = 'a' THEN 1 END) AS a,
       COUNT(CASE WHEN left(w.wd, 1) = 'b' THEN 1 END) AS b,
       COUNT(CASE WHEN left(w.wd, 1) = 'c' THEN 1 END) AS c
  FROM ptbr w

If you want to return the words:

SELECT left(w.wd, 1) AS letra,
       GROUP_CONCAT(w.wd SEPARATOR ',') AS palavras,
       COUNT(CASE WHEN left(w.wd, 1) = 'c' THEN 1 END) AS c
  FROM ptbr w
 GROUP BY left(w.wd, 1)

If you want to separate by columns:

SELECT GROUP_CONCAT(IF(left(w.wd, 1) = 'a', w.wd ,'') SEPARATOR ', ') AS palavrasComA,
       GROUP_CONCAT(IF(left(w.wd, 1) = 'b', w.wd ,'') SEPARATOR ', ') AS palavrasComB,
       GROUP_CONCAT(IF(left(w.wd, 1) = 'c', w.wd ,'') SEPARATOR ', ') AS palavrasComC
  FROM ptbr w

Or if you just want the first word:

SELECT MIN(IF(left(w.wd, 1) = 'a', w.wd, NULL)) AS palavrasComA,
       MIN(IF(left(w.wd, 1) = 'b', w.wd, NULL)) AS palavrasComB,
       MIN(IF(left(w.wd, 1) = 'c', w.wd, NULL)) AS palavrasComC,
  FROM ptbr w
  • In this case here only returned the number, and not the words.

  • @Marcusviniciustavares added a second option, please check

  • All words with the letter 'a' in the column 'pa',

  • All words with the letter 'b' in the pb column',

  • All words are in the same table that is a dicynnarium

  • @Marcusviniciustavares check the third option

  • The third is almost perfect for my case. Porrem would like a word per line in each column.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.