Validate mysql query if NULL - php

Is this query possible in mysql?
If query returns null, then set x = false else set x = true
Query
SET #s1 = (SELECT * FROM `wo_ticket_details_replay`
WHERE message NOT LIKE "[ SYSTEM%" AND `sv_number`='0715201569998')
CASE WHEN #s1 = NULL THEN
x = 'false'
ELSE
x = 'true'
END CASE;

You can use a subquery with EXISTS.

Related

SQL Server query on php

I'm begginner in this field so I need help.
I want to get integer value from this select. When I run this query in sql server - it returns correct value (so that part is okay)
My idea is to get integer value of $select -> put it in $value variable and use it after that.
Here is working sql expression:
$select = "SELECT COUNT (*) broj (SELECT a.CaseNo as CaseNo,
b.Naruc as Naruc,
b.Opis as Opis,
b.Vrijednost as cijena,
CONVERT(VARCHAR(50),b.Datumpn, 127) as datum_predaje,
(case when b.Kn IS NULL then 0 when b.Kn IS not null then b.Kn end) as kat1,
(case when b.Kn2 IS NULL then 0 when b.Kn2 IS not null then b.Kn2 end) as kat2,
(case when b.Kn3 IS NULL then 0 when b.Kn3 IS not null then b.Kn3 end) as kat3,
a.DateInserted as datum_unosa
FROM [Therefore].[dbo].[za_slanje_maila] a, [Therefore].[dbo].[TheCase1] b
WHERE CONVERT(date,a.DateInserted) = '".$today."'
AND a.DateInserted < '".$today3."'
AND a.CaseNo = b.CaseNo
AND b.Naruc is not null
AND b.Opis is not null
AND b.Vrijednost is not null
AND b.Datum_predaje is not null) broj";
After that, I tryed this
$data_api = sqlsrv_query($conn, $select, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
And then I try to call
$value = $data_api['broj']
- but that is empty.
How can I assign that value to the variable $value?
Thanks.
sqlsrv_query() doesn't return an array yet, but statement resource (or false on error). You can then use this resource to get the data array with sqlsrv_fetch_array() function.

Laravel/Postgres db queries?

I have (weird) problem with query on Laravel/Postgres DB Queries. I will paste entire query here(it's little too long but we will focus on just one part of it):
select distinct "products".*, "brands"."name" as "brand", "brands"."image" as "brand_image", "product_categories"."name" as "category", "product_categories"."slug" as "category_slug", count(distinct product_features.feature_category_id) as features_count from "products" inner join "brands" on "products"."brand_id" = "brands"."id" inner join "product_categories" on "products"."category_id" = "product_categories"."id" left join "product_features" on "products"."id" = "product_features"."product_id" where "products"."deleted_at" is null and "products"."category_id" in (142) and "products"."published" = true and "brands"."published" = true and "product_categories"."published" = true and "product_categories"."deleted_at" is null and "price" >= 0 and "price" <= 19000 and "products"."brand_id" in (17) and
(product_features.feature_category_id = 555 and
CAST(product_features.value AS BOOLEAN) = true and
product_features.deleted_at IS NULL) or
(product_features.feature_category_id = 554 and
CAST(product_features.value AS BOOLEAN) = true and
product_features.deleted_at IS NULL) group by "products"."id", "brands"."name", "brands"."image", "product_categories"."name", "product_categories"."slug" having count(distinct product_features.feature_category_id) = 2 order by price asc
This is initial query but when I execute it I get weird error said:
ERROR: invalid input syntax for type boolean: "800"
I have no idea why is this error thrown?
Your product_features.value is a text field. While Postgres is OK casting all nonzero integers like 800 to boolean true, it is not OK with casting text like '800' to boolean.
Solution is to check product_features.value on emptiness and/or equality to '0' instead of CAST(product_features.value AS BOOLEAN) = true.

mysql query with if and else statement

I need mysql query that will :
IF (result=win){SELECT dobitak} else if (result=half_win) {SELECT dobitak/2} else if (half_lost) {SELECT stake/2} else{0};
FROM matches
WHERE id=$id
but i dont know whats the best solution to write query like this.
select case when result ='win' then dobitak
when result = 'half_win' then dobitak/2
when result = 'half_lost' then stake/2
else 0
end as result
FROM matches
WHERE id=$id
I think you can do what you want with a case statement:
select (case when result = win then dobitak
when result = half_win then dobitak/2
when half_lost then stake / 2
else 0
end)
from matches
where id = $id;

counting null columns Mysql using PHP

I am wanting to count the null columns or with a certain value
$q = mysql_query("
SELECT SUM(
(`foto` = 'avatar_small.png') +
(`email` IS NULL) +
(`cidade` IS NULL) +
(`estado` IS NULL) +
(`endereco` IS NULL) +
(`numero` IS NULL) +
(`cep` IS NULL) +
(`telefone` IS NULL)
) FROM `info_complementos` WHERE id_user = ".$_SESSION['usuario_cargo']) or die(mysql_error());
$retorno = mysql_num_rows($q);
return $retorno[0];
the problem is that it shows the result
You can use the COUNT function in mysql to count the rows returned. So you want to filter out the rows that you do not want in the WHERE clause. This will count all the rows that match ALL the conditions. You can change the AND to OR if you want to match rows that contain some NULL values.
<?php
$q = mysql_query("
SELECT COUNT(*) FROM `info_complementos`
WHERE
id_user = $_SESSION['usuario_cargo'] AND
`foto` = 'avatar_small.png' AND
`email` IS NULL AND
`cidade` IS NULL) AND
`estado` IS NULL) AND
`endereco` IS NULL AND
`numero` IS NULL AND
`cep` IS NULL AND
`telefone` IS NULL");
$result = mysql_fetch_array($q);
echo $result[0];
Hope this helps.

MySQL EXISTS return 1 or 0

How can I return the 1 or 0 as true and false for my select query? I've tried every combination of fetch_assoc, arrays, etc. It usually returns as null, which doesn't make sense.
$querydetails = "select exists (select * from customer_det where id = $uid)";
$resultdetails = mysql_query($querydetails) or die(mysql_error());
while ($row = mysql_fetch_assoc($resultdetails)) {
$resultdetails1 = $row[0];
}
That's the latest version of my query. I guess it's not an array, but I'm not sure how to pull the value if there's no row to be named?
This is a post question to my previous thread: MySQL Update WHERE
SELECT IF(EXISTS(...),1,0) AS result
simple use COUNT and IF
SELECT IF(COUNT(*) = 0, 0, 1)
FROM table1
WHERE s = 2
SQLFiddle Demo
SELECT count(*) FROM (
SELECT id FROM table WHERE condition LIMIT 1
) AS result;
i am using mysql version 8.0.23 on AMS
SELECT
column1,
column2,
IF (exists(select * from MeasurementItemNos measureItem where measureItem.itemNo = ib.itemNo and measureItem.IsActive = 1),1,0) as isUploadedSizeSpec
from table
I would do:
select * from customer_det where id = $uid LIMIT 1
You will get either 1 row or zero rows.

Categories