I have a query
SELECT *
FROM hayabusa.customer_staging
WHERE tin = "888-596-592"
OR gsis_sss_no = "bp0279638"
OR last_name LIKE "%dela cruz%"
OR first_name LIKE "%jose%"
OR date_of_birth = "8/8/1978"
OR gender = "male"
AND parent_id = 0
AND is_proccessed = 0
AND id > 27
AND ( filename = "sample cif csv file - copy.csv"
OR filename = "sample cif csv file2.csv" )
GROUP BY tin,
gsis_sss_no,
last_name,
first_name,
date_of_birth,
gender
ORDER BY CASE
WHEN tin = "888-596-592" THEN 1
ELSE 2
end,
id
LIMIT 1;
But it's returning a row with an id of 3 and a filename which is not in the where clause. What is wrong with it?
I believe that AND has higher precedence than OR in MySQL (and most databases), so your current query is treating your AND condition with the filename as being optional. Try rewriting the WHERE clause as this:
WHERE
(tin = "888-596-592"
OR gsis_sss_no = "bp0279638"
OR last_name LIKE "%dela cruz%"
OR first_name LIKE "%jose%"
OR date_of_birth = "8/8/1978"
OR gender = "male")
AND parent_id = 0
AND is_proccessed = 0
AND id > 27
AND ( filename = "sample cif csv file - copy.csv"
OR filename = "sample cif csv file2.csv" )
Also, I don't know which columns you intend to select, but using SELECT * here is probably inappropriate. Instead, you should list out which columns you want to select. Strictly speaking, only columns appearing in the GROUP BY clause or columns which are inside aggregate functions are eligible for selection in your query.
Related
For example I have a variable $num = 123; and another one called `$name=joe;' , and there is a Database that contains a table called "data" and inside this table there are two columns (num [type=varchar(255)] - name[type=varchar(255)]) .
For example these query exists in the DB :
num = 123456 , name = joe
How to make a check that the first "3" numbers equals the $num variable and the name equals variable $name ?
I tried this but it didn't work:
SELECT * FROM data Where SUBSTRING(num , 0 , 3) = '123' AND name = 'joe'
In MySQL, substring indexing starts at 1:
WHERE SUBSTRING(num , 1 , 3) = '123' AND name = 'joe'
But LEFT() or LIKE would more commonly be used:
WHERE LEFT(num , 3) = '123' AND name = 'joe'
WHERE num = '123%' AND name = 'joe'
The advantage of LIKE is that it can make use of an index . . . even one on (name, num).
The MySQL substring() function is 1-based, not 0-based, so this should work for you:
SELECT * FROM data Where SUBSTRING(num , 1 , 3) = '123' AND name = 'joe'
I have to recreate a stored procedure which existed in DB1, and map the existing tables to new tables in the new database DB2.
In DB1, table there was column Fullname and in the new db there are two columns firstname, lastname. I could have concat but there is a user defined function also which truncates all special characters which I have to use.
How do I use the first name and last name columns as one full name column so that it fits this case statement?
I am getting the [P Name] from another table enrolled enr which is like the master table having all the names, getting names from either tables is conditioned as shown in case statement.
I tried searching all blogs and stackexchange but cannot get the desired reply.
masterfinancer = dbo.fn_RemoveSpecialChars(iif(enr.p_name = 'XXXXXXXXX,XXXXXXX(2)', 'XXXXX XXXX XXX GROUP', mv.master_vendor_name))
Sma_finace_key = iif(enr.p_name = 'XXXXXXXXX0L(2)', 1111, mv.ven_key)
[P Name] = case
when enr.p_name = 'INACTIVE ' or enr.p_name = 'UNASSIGNED'
then [P Name]
else dbo.fn_RemoveSpecialChars(enr.p_name)
end
You'll need to use the function on each separate field and join them together. Here I removed the IIF statements (which only work in SQL 2012) and created a FullName field that assumes you want "First, Last". I added COALESCE to avoid the FullName field being NULL when either the first or last names are NULL:
SELECT
masterfinancer = dbo.fn_RemoveSpecialChars(
CASE
WHEN enr.p_name = 'XXXXXXXXX,XXXXXXX(2)'
THEN 'XXXXX XXXX XXX GROUP'
ELSE mv.master_vendor_name
END)
, Sma_finace_key =
CASE
WHEN enr.p_name = 'XXXXXXXXX0L(2)'
THEN 1111
ELSE mv.ven_key
END
, [P Name] =
CASE
WHEN enr.p_name IN('INACTIVE ', 'UNASSIGNED')
THEN [P Name]
ELSE dbo.fn_RemoveSpecialChars(enr.p_name)
END
, FullName =
COALESCE(dbo.fn_RemoveSpecialChars(enr.last_name) + ', ', '') +
COALESCE(dbo.fn_RemoveSpecialChars(enr.first_name), '')
I have to do a query SQL to search into the table below
Person
Name - Surname - Age
using Name, Surname ad Age as parameter of my query.
Since this query is generated dinamically, may happen that one of this parameters is equal to "" or null. In this case I expect that the behavior obtained is:
If Name = "", I want to search for surname and age regardless of the
name (every name).
I have found a possible solution to this problem and is to use a LIKE statement, in this way:
SELECT * ...
WHERE Surname like '%%' AND Name like '%%' AND Age like '%%'
When I put %% in the like it returns me all records of the table.
Is this correct or there is another way?
SQL offers methods for those issues.
You can easily use (Surname IS NULL OR Name = '' OR ...)
See: https://dev.mysql.com/doc/refman/5.7/en/is-null-optimization.html
Theres also a thread with almost the same question: MySQL syntax checking if parameter is null
...
Where (IsNull(#SurName, '') = '' OR #SurName = t.SurName)
AND (IsNull(#Name, '') = '' OR #Name = t.Name)
AND (IsNull(#Age, '') = '' OR #Age = t.Age)
For sql server it could look something like this:
select *
from t
where (
Surname like '%'+#Surname+'%'
or coalesce(#Surname,'') = ''
)
and (
Name like '%'+#Name+'%'
or coalesce(#Name,'') = ''
)
and (
Age like '%'+#Age+'%'
or coalesce(#Age,'') = ''
)
catch-all queries
Dynamic Search Conditions - Erland Sommarskog
Catch-all queries - Gail Shaw
SELECT * ...
WHERE
IF(NAME IS NULL OR NAME='',Surname LIKE '%%' AND Age LIKE '%%', Surname LIKE '%%' AND NAME LIKE '%%' AND Age LIKE '%%');
I am having the following issue:
When I execute the following statement, I get an error for it returning more than one row
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow'),
(SELECT musicgenreid
FROM musicgenre
WHERE musicgenreid = 4),
( totallikes = 328374 ) );
I am getting the error on the (select pesonID from person...) statement, and I am trying to use the 'any' keyword to just grab any row, but I cannot seem to get it to work. I have tried just about any permutation I can think of of the current query and 'any', but it does not work. Is there another solution I should be trying or am I just missing the mark for some reason?
It seems you're trying to do something like this:
INSERT INTO artist (personid, musicgenreid, totallikes)
VALUES (
(SELECT personid FROM person
WHERE firstname = 'Joe' AND middlename = '' AND lastname = 'blow'
ORDER BY RAND()
LIMIT 1
),
4,
328374
);
This will get you a random personid that matches the given criteria.
The musicgenreid in your query would be either null or 4. I am forcing it to 4 as it seems that you're manually adding them and you know they already exist.
The total likes field is also fixed but your syntax was incorrect.
try with this sql statement
INSERT INTO artist
(personid,
musicgenreid,
totallikes)
VALUES ( (SELECT personid
FROM person
WHERE firstname = 'Joe'
AND middlename = ''
AND lastname = 'blow' LIMIT 1 ),
4,
328374);
I have the following sql that I run in C:
snprintf(sql, 200, "update rec set name = (select name from pers where id = %d )
where id = %d",rec_id , emp_id );
mysql_query(conn, sql) returns a successful result but it's putting 1 in the "rec" table in the "name" field instead of the name, but when I printf the output and use it in MySQL it's working fine.
update rec set name = (select name from pers where id = 104 ) where id = 43
Is there something wrong with my sprintf? Or something has to be added?
I also tried static sql command like this
snprintf(sql,"update rec set name = (select name from pers where id = 104 ) where id = 43");
and it also put 1 in the rec.name
Is that due to count of record returned by the sub query? Can you verify by putting a condition which returns e.g. 2 records so that the name is set to 2? if this is the reason then (though less performing approach) try splitting the queries and see if it works this time.