I have a simple table called unzip. It has three columns as follows -: id INT UNSIGNED NOT NULL AUTO_INCREMENT, share_src VARCHAR(30) NULL, creator INT UNSIGNED NOT NULL.
I also have a function called checkSrc which returns no or yes when a share_src is provided to it as a parameter.
I want to return all the rows where creator is 10 AND (share_src is null or
the result from the function checkSrc is yes).
Currently i am doing this
SELECT * FROM unzip WHERE creator = 12 AND (share_src IS NULL || checkSrc(share_src) = 'yes');
But this results in an error. Whats the problem in the query? A lot thanks for help..
SELECT * FROM unzip WHERE creator = 12 AND (share_src IS NULL Or checkSrc(share_src) = 'yes');
Try and let me know.
Check if function is returning correct data type. Check if its returning string type.
Related
I have a query where there are 2 conditions used one for id and another for project_id , id is type int(10) and project_id is type varchar(250) and I having issue with this query please see the image. According to normal login it should only return the row with id 57 but its returning 2 rows which wrong. Can anyone please help me out
Try it
select * from `projects` where `id` = 57 and `project_id` = '7QJYouUh'
You have input wrong id And you want to log in so, you to set condition 'and'.
In the below query I am saving into data base an array from multiple select2 options.
Here i have a database table participants with event_id(int, not null), user_id(int, null), p_mail(varchar, null).
When I pass proper id which is in users the data gets saved properly and null will be displayed in p_email column.
But when i pass some varchar values through array, the user_id is saved as 0 which should be NULL and Null is stored in p_email which should store the values.
foreach($resources as $r) {
global $response;
$sql = "INSERT INTO participants(event_id, user_id, p_email)
SELECT $response,
CASE
WHEN EXISTS (SELECT * from users where id = '$r') THEN
'$r'
ELSE NULL
END,
CASE
WHEN EXISTS (SELECT * from users where id = '$r') THEN
NULL
ELSE '$r'
END
FROM users WHERE id = 1;";
$query_result = $conn->query($sql) or die("Failed".$sql);
this is expected where it int is passed it should be stored in user_id column if id exists otherwise if its varchar it should be stored in p_email.
Present if I store a varchar which is not in users after checking instead of storing it in p_email. it stores null in p_email and 0 in user_id.
I think the issue here is that the id column is numeric. In the first test, you are presumably passing some legitimate numerical string id, such as '123'. MySQL has no issue internally converting between '123', the string, and 123, the number. However, in your second test, you are passing something like abc. In this case, MySQL appears to be converting that to a numeric value of 0. Also, assuming the id zero does exist, you would see NULL in your email column as well.
So, I think the correction here is to just bind numbers to your insert query, assuming the id column is really numeric.
And, you should read about using prepared statements in MySQL.
I cleared this issue by using LIKE instead of '=' in the code.
I have a table that has a foreign key to itself to keep track of ParentID. This is used to make a treelike hierarchy in the database. I'm trying to create a function that will find all rows that have a particular ParentID. However, if a record is a Root record it's parent is NULL. Therefore if I want to find all root tables I need to be able to enter NULL as an option as well. What I have so far is:
SELECT * FROM X.Y
WHERE
CASE
WHEN #ParentID IS NULL THEN ParentID IS NULL
WHEN #ParentID IS NOT NULL THEN ParentID = #ParentID
END
I think I'm on the right track, but haven't quite figured it out. It is saying Incorrect syntax near the keyword 'IS' indicating the IS in THEN ParentID IS NULL.
Any help would be great.
You're close. Try this:
SELECT * FROM X.Y
WHERE (#ParentID IS NULL AND ParentID IS NULL )
OR (ParentID = #ParentID )
About the first clause in my WHERE statement: any comparison to a NULL or a field whose value is NULL will always return false. Even WHERE NULL = NULL will evaluate as false.
So if you want to say, "Do this if both my variable and the field itself are null" you need to use something like WHERE (#ParentID IS NULL AND ParentID IS NULL ).
Some people like to do something like this:
WHERE (ISNULL(#ParentID,0) = ISNULL(ParentID,0)
It's tidy, but it doesn't allow the query engine to take advantages of the indexes that you might have on your table. It's considered a bad idea.
Try replacing the second WHEN to an ELSE or simply using an or like this
SELECT * FROM X.Y
WHERE ParentID IS NULL OR ParentID = #ParentID
I have table Clients that contains many columns, however I'm targeting two particular columns ClientId and PIN. The example that I'm going to provide is not making much sense but I'm just learning more advanced SQL (T-SQL) topics and my final goal is something a lot different than what is here.
So I made a function which I would like to return all rows that match certain search criteria. So I have this:
CREATE FUNCTION ufn_NumberOfClients (#ClientId INT)
RETURNS #retContactInformation TABLE
(
-- Columns returned by the function
ClId int PRIMARY KEY NOT NULL,
PIN nvarchar(50) NULL
)
AS
BEGIN
DECLARE
#ClId INT,
#PIN nvarchar(50)
SELECT #ClId = ClientId, #PIN = PIN
FROM Client
WHERE PIN LIKE '%7788%'
IF #ClientId IS NOT NULL
BEGIN
INSERT #retContactInformation
SELECT #ClId, #PIN;
END;
RETURN;
END
But when I execute the function:
SELECT * FROM
ufn_NumberOfClients(4)
I get only one result. Even though it's a valid result, if I execute just
SELECT * FROM Client
WHERE PIN LIKE '%7788%'
I get 5 results, and the function is returning just the last one.
Two thing which worth mentioning. The example which I use to write the function the ClId int PRIMARY KEY NOT NULL, from the table is not DECLAREed after that, but I got error if I don't do it. Also, I pass variable, but I think it's not used so it shouldn't affect the behaviour (or at least I think so).
So how can I return all the results matching the search crieria with a function?
You actually insert only one row in the table variable right now, Try this instead :
CREATE FUNCTION ufn_NumberOfClients (#ClientId INT)
RETURNS #retContactInformation TABLE
(
-- Columns returned by the function
ClId int PRIMARY KEY NOT NULL,
PIN nvarchar(50) NULL
)
AS
BEGIN
INSERT INTO #retContactInformation
SELECT ClientId, PIN
FROM Client
WHERE PIN LIKE '%7788%';
RETURN;
END
I tried to implement the following mysql command try to get rows with none null values,
here is the table tbtext:
id text
1
2 number2
3 number3
4
create table tbtext(
id unsigned int auto_increment primary key,
text char(7)
)
I tried select * from tbtext where text is not null, but it returns the whole rows and doesn't return row 3 and row 4. I am new to mysql, can anyone help me with it, thanks in advance.
I'm assuming text contains empty strings, and not NULL. The best thing to do would be to replace those empty strings with NULL, but you could also use this query :
-- ...
WHERE text <> '';
Please note however that empty strings are information, where NULL means lack of information. You should definitely not have empty strings if that's not what you're trying to do.