WHERE clause stored in Mysql Field - mysql

I'm using OpenEMM, and I'd like to edit a second database with a trigger.
I'd like to get all users of a target.
I have two tables :
user
target which contains a field "target_sql", it's the where clause of the SQL request to retrieve all users of the target
For example : a field in target_sql :
( ( lower(cust.EMAIL) = lower('test#gmail.com')) )
I tried
SELECT * FROM user WHERE target_sql;
And
DECLARE targetsql TEXT;
SELECT target_sql FROM target WHERE target_id=1 INTO targetsql;
Both doesn't work.
Do you have an idea, how can I do that ?

Related

SELECT statement inside a CASE statement in SNOWFLAKE

I have a query where i have "TEST"."TABLE" LEFT JOINED to PUBLIC."SchemaKey". Now in my final select statement i have a case statement where i check if c."Type" = 'FOREIGN' then i want to grab a value from another table but the table name value i am using in that select statement is coming from the left joined table column value. I've tried multiple ways to get to work but i keep getting an error, although if i hard code the table name it seems to work. i need the table name to come from c."FullParentTableName". Is what i am trying to achieve possible in snowflake and is there a way to make this work ? any help would be appreciated !
SELECT
c."ParentColumn",
c."FullParentTableName",
a."new_value",
a."column_name"
CASE WHEN c."Type" = 'FOREIGN' THEN (SELECT "Name" FROM TABLE(c."FullParentTableName") WHERE "Id" = 'SOME_ID') ELSE null END "TestColumn" -- Need assistance on this line...
FROM "TEST"."TABLE" a
LEFT JOIN (
select s."Type", s."ParentSchema", s."ParentTable", s."ParentColumn", concat(s."ParentSchema",'.','"',s."ParentTable",'"') "FullParentTableName",s."ChildSchema", s."ChildTable", trim(s."ChildColumn",'"') "ChildColumn"
from PUBLIC."SchemaKey" as s
where s."Type" = 'FOREIGN'
and s."ChildTable" = 'SOMETABLENAME'
and "ChildSchema" = 'SOMESCHEMANAME'
) c
on a."column_name" = c."ChildColumn"
Thanks !
In Snowflake you cannot dynamically use the partial results as tables.
You can use a single bound value via identifier to bind a value to table name
But you could write a Snowflake Scripting but it would need to explicitly join the N tables. Thus if you N is fixed, you should just join those.

No database selected Select the default DB to be used by double-clicking

I am writing a SQL query and I have an array in one table and in that array I stores IDs and want to compare that array of IDs with another table to show data against that ids. when I run it gives me following error.
No database selected Select the default DB to be used by double-clicking.
Here is my query
select TagId
, Name
from ctrData2.Tag
Left
outer join ctrData2.CallDetail
On Tag.TagId = array(CallDetail.Tag)
where CallDetail.ContactId = 'f9d4787a-f1ac-41af-97d8-ea324daad018'
this is how I store IDs in array in Tag Column
here is the other table from where I want to show data against these ids
you can set the default schema by below query first
use ctrData2;
And run below query
select TagId, Name
from Tag, CallDetail
where CallDetail.ContactId = 'f9d4787a-f1ac-41af-97d8-ea324daad018' and LOCATE(Tag.TagId, CallDetail.Tag) > 0;

How to make where condition always be true with field =?

I have a sql statement follow:
select * from table where id = ?
Now, problem is, l don't know whether front end will send me the value of id, if it did, this sql seem like id = 1, and if not, sql should be like id = true(fake code) to find all data
How could I write my sql?
Or, It is fundamentally wrong?
This is normally handled by using logic such as this:
select *
from table
where id = ? or ? is null;
If you don't want to pass the parameter twice or use named parameters:
select t.*
from table t cross join
(select ? as param) params
where id = params.param or params.param is null;
If you want to return all ids if the passed-in value does not exist:
select t.*
from table t
where id = ? or
not exists (select 1 from table t2 where t2.id = ?);
What you can try doing is in your code, write a function for fetching a specific record, and another function for fetching all the records from your table.
In PHP, it could be something like:
// Fetching a specific record
function getCustomerRecord($customerId) {
// Code to fetch specific record from database
}
// Fetching all records
function getAllCustomerRecords() {
// Code to fetch all records from database
}
In the function where you process requests received, check first if a value for id was passed. If a value for id was passed, call the function to fetch a specific record, making sure to pass along the value you received as an argument. Otherwise, call the function to fetch all the records from your table.
You can try doing this to get your right sql statement in PHP
function GetSqlStatement($id){
return $sql = "select * from table where id = ".$id.";";
}

How to make if statement in query to mysql from excel

I am trying to make an if statement in an excelcommand, so I only get data from rows where a column named active is equal to 0.
The following command works fine, and returns everything from my "atests" tabel on my mysql server.
SELECT * FROM `DrLau_MISB`.`atests` WHERE userId = ?
(where ? is defined in another cell)
I wan't to only get data from the rows there the column named active = 0.
is it possible to make something like this
SELECT * FROM `DrLau_MISB`.`atests` WHERE userId = ? AND active IS 0
SELECT * FROM `DrLau_MISB`.`atests` WHERE userId = ? AND active = 0;

Store procedure select all fields from One table using join

I am very frustrated from linq to sql when dealing with many to many relationship with the skip extension. It doesn't allow me to use joinned queries. Not sure it is the case for SQL server 2005 but I am currently using SQL Server 2000.
Now I consider to write a store procedure to fetch a table that is matched by two tables e.g. Album_Photo (Album->Album_Photo<-Photo) and Photo table and only want the Photos data so I match the Album's ID with Album_Photo and use that ID to match the photo. In the store procedure I am just fetch all the joinned data. After that in the linq to sql, I create a new Album object.
e.g.
var albums = (from r in result
where (modifier_id == r.ModifierID || user_id == r.UserID)
select new Album() {
Name = r.Name,
UserID = r.UserID,
ModifierID = r.ModifierID,
ID = r.ID,
DateCreated = r.DateCreated,
Description = r.Description,
Filename = r.Filename
}).AsQueryable();
I used the AsQueryable to get the result as a IQueryable rather than IEnumerable. Later I want to do something with the collection, it gives me this error:
System.InvalidOperationException: The query results cannot be enumerated more than once.
It sounds like you have a situation where the query has already executed by the time you are want to filter it later in your code.
Can you do something like...
var albums = (blah blah blah).AsQueryable().Where(filterClause) when you have enough info to process
what happens if you try albums.where(filter) later on in the code? Is this what you are trying?