Where mistake in sql LIKE clause? - mysql

I try to count all items from another table with this select:
SELECT id, name, (SELECT count(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
and prekes_main.pg_kodas LIKE 'grupes_main.pg_kodas%') as pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
and grupes_main.name <> ''
In LIKE clause I want automatically get selected grupes_main column pg_kodas, but in this query it always returns 0, where is mistake in LIKE function? thx

SELECT id, name,
(
SELECT COUNT(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
AND prekes_main.pg_kodas LIKE CONCAT(grupes_main.pg_kodas, '%')
) pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
AND grupes_main.name <> ''

Related

Add the result of SELECT query in another SELECT query

This is the result of a UNION of two SELECT
SELECT count(*) FROM
((SELECT session_id_current_user from test.tws_analytics
WHERE (add_date BETWEEN '2022-05-15' AND '2022-05-15') AND ((pathURL='vues/login.php' AND name_current_user='') OR (pathURL='' AND searchURL='?job=forgotten' AND name_current_user=''))
AND session_id_current_user NOT IN
(SELECT session_id_current_user from test.tws_analytics
WHERE (pathURL <> 'vues/login.php' AND searchURL <> '?job=forgotten') AND add_date BETWEEN '2022-05-15' AND '2022-05-15' order by session_id_current_user)
order by session_id_current_user)
UNION
(SELECT name_current_user from test.tws_analytics where add_date BETWEEN '2022-05-15' AND '2022-05-15' AND name_current_user IS NOT NULL AND name_current_user <> ''))
AS tem
The result is 11.
What I want to do is to select this result with other columns like this :
SELECT count(session),count(name), [AND tem.count(*)] FROM ....
This is the general idea, though i didn't know how to implement it.
a simplified general answer would be
select * from (select count(*) numsessions from sessions), (select count(*) numusers from users)
this will give 2 different counts, i didn't include the logics that you provided, but that will need to be done inside the 2 subqueries.

Second SELECT query in another table if first SELECT returns 0 rows

I already read this question. But this is on same table. How can I archie that in two or three table like :
if (SELECT ViewCode FROM M_VIEW
WHERE ViewCode=?) //if found return
else (SELECT ViewCode FROM M_Customer
WHERE CustomerCode=?)
Here is another way of doing it, it'll return rows from exactly one table:
SELECT ViewCode FROM M_VIEW WHERE ViewCode = #ViewCode
UNION ALL
SELECT ViewCode FROM M_Customer WHERE CustomerCode = #CustomerCode AND NOT EXISTS (
SELECT 1 FROM M_VIEW WHERE ViewCode = #ViewCode
)
Note that I've replaced ? with variable names to show which ? means what.
Demo on db<>fiddle
You can use a solution like the following:
SELECT DISTINCT ViewCode FROM (
SELECT ViewCode, 'M_VIEW' AS tName FROM M_VIEW WHERE ViewCode = 1
UNION ALL
SELECT ViewCode, 'M_Customer' FROM M_Customer WHERE ViewCode = 1
) t GROUP BY tName, ViewCode
HAVING tName = CASE
WHEN SUM(tName = 'M_VIEW') > 0 THEN 'M_VIEW'
WHEN SUM(tName = 'M_Customer') > 0 THEN 'M_Customer'
ELSE ''
END
demo on dbfiddle.uk
Assuming that each query should return 0 or 1 row, you could use union all, then order the records and limit, as follows:
SELECT ViewCode
FROM (
SELECT ViewCode, 1 seq FROM M_View WHERE ViewCode = ?
UNION ALL SELECT ViewCode, 2 FROM M_Customer WHERE CustomerCode = ?
) t
ORDER BY seq
LIMIT 1
If the first query (from M_View) returns a record, the ordering clause puts it first, and limit 1eliminates the potential other record. Else, the (only) matching record (from M_Customer) will be selected.

SQL Subquery IN SELECT [Symfony3]

I Have Table Actions with the following fields :
idAction
Cloture
Date
I'd Like to loop through and display a list of all idAction In my DB + idAction with Cloture = 0 Group By the same Date (both of them).
I tried below method. But it doesn't work. Can anyone help me?
$query = $this->getEntityManager()->createQuery(
'SELECT COUNT(a.idAction) AS nmbreAction , week(a.dateOuverture) AS week,( SELECT COUNT(c.idAction) , week(c.dateOuverture) FROM ActionActionBundle:Action c
WHERE c.cloture = 0 ) AS nmbreRetard FROM ActionActionBundle:Action a
GROUP BY week');
Mmm, you question lacks a lot of information. Is this what you need?
SELECT COUNT(a.idAction) AS nmbreAction ,
week(a.dateOuverture) AS week,
(SELECT COUNT(c.idAction)
FROM ActionActionBundle:Action c
WHERE c.cloture = 0
and week(c.dateOuverture) = week(a.dateOuverture)) AS nmbreRetard
FROM ActionActionBundle:Action a
GROUP BY week(a.dateOuverture)
You can't select more than 1 column in a sub query\correlated query in the select list, which was probably showed to you in the error message.
EDIT: Better of do that:
SELECT COUNT(a.idAction) AS nmbreAction ,
week(a.dateOuverture) AS week,
COUNT(CASE WHEN a.cloture = 0 THEN 1 END) as nmbreRetard
FROM ActionActionBundle:Action a
GROUP BY week(a.dateOuverture)

Limit 1 of each from different column if conditions

Is there a way to say, select 2 rows from the database, where one column equals something, or one column equals something else, but you want one row each from EACH equal conditions? So...
SELECT * FROM tableName WHERE colName = '1' OR colName = '2' LIMIT...
1 where colName = '1', and 1 where colName = '2'. So, one of each.
If you have the values, then use union all:
(select t.*
from tablename t
where colname = '1'
limit 1)
union all
(select t.*
from tablename t
where colname = '2'
limit 1)

how do I write an IF ELSE into this query to make it work?

table looks something like this: (yes those are & signs. ignore the dashes)
ID-VALUE-NUM
-1-YES----2-
-1-NO-----3-
-2-YES----1-
-2-NO-----1-
-3-&&&----1-
-3-&------2-
-3-&&-----2-
what I need to do:
for each ID, I need to get the value with the highest NUM, in the case of a tie and VALUE has &s then it would pick the shortest. if the value is YES/NO then it will pick YES.
result desired
ID-VALUE-NUM
-1-NO-----3-
-2-YES----1-
-3-&------2-
I think I have to put a IF statement in there somewhere but I'm not sure how.
Here is one way. The join finds the maximum num. Then the select uses logic to choose the right value based on your rules:
select t.id,
(case when count(*) = 1 then min(value)
when max(value like '%&%') > 0 then min(value)
when max(value = 'Yes') > 0 and max(value = 'No') > 0 then 'Yes'
else max(value)
end) as value,
t.num
from t join
(select id, max(num) as maxnum
from t
group by id
) tm
on t.id = tm.id and t.num = tm.maxnum
group by t.id, t.num