Limit 1 of each from different column if conditions - mysql

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)

Related

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.

select ifnull() won't let more than 1 column in the query

I get this error - 1241, operand should contain 1 column(s), upon running the query below:
select ifnull((select col1, col2 from table where uid = num limit 1) , '0');
If I project only 1 column it runs without error, I actually want to use select * to project all the columns, but it's not working for than one column, please suggest me something.
Try like this:-
select ifnull(col1,'0'), ifnull(col2,'0') from table where uid = num limit 1
You can't use ifnull over two column you could
or check for one column
select ifnull((select col1 from table where uid = num limit 1) , '0');
or use case for eval the content and return a single value
select ifnull((select case when col1 is null and col2 is null then null else 1 end
from table where uid = num limit 1) , '0');

How to calculate Sum of 2 Count(*) in Mysql

I have two tables that I count rows of them.
SELECT COUNT(*)
FROM docgrados_directores
WHERE docgrados_directoresleido = '0' AND docgrados_directoresusu = '11'
result 1
SELECT COUNT(*)
FROM docgrados_lectores
WHERE docgrados_lectoresleido = '0' AND docgrados_lectoresusu = '11'
result 1
I need total count (result would be 2). How can I sum the result with a single statement? What is the correct syntax??
Use another SELECT to add the scalar values returned by your queries:
SELECT (SELECT COUNT(*)
FROM docgrados_directores
WHERE docgrados_directoresleido = '0' AND docgrados_directoresusu = '11' )
+
(SELECT COUNT(*)
FROM docgrados_lectores
WHERE docgrados_lectoresleido = '0' AND docgrados_lectoresusu = '11')
The above statement should return 2 as result if the result of both subqueries is a 1.

MySQL where clause that should not work but returns all table contents

This query is returning all the table contents. To me is not a valid where clause. Anyone have any idea why this works?
SELECT * FROM TableName WHERE 1
In MySQL, TRUE is a costant value = 1, while FALSE is = 0, your query is then eqivalent to:
SELECT * FROM TableName WHERE TRUE
also, all conditions are converted either to 0 or to 1:
SELECT 'a' = 'a'
will return 1, while
SELECT 'a' = 'b'
will return 0 so for example the following queries are all equivalent:
SELECT * FROM TableName WHERE TRUE
SELECT * FROM TableName WHERE 'a' = 'a'
SELECT * FROM TableName WHERE 1
but every value <> 0 is considered true as well, so even this will return all rows:
SELECT * FROM TableName WHERE 2
but if a value <> is considered true, one would expect the following query to work:
SELECT * FROM TableName WHERE 2 = TRUE
but this won't return anything, because 2 = 1. Yes, sometimes MySQL is a little weird.
Select * from TableName where 1 is equivalent with Select * from TableName because it mens where true

Where mistake in sql LIKE clause?

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 <> ''