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);
Related
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.
I want to insert a data only if it doesnt already exists. heres how i do :
IF NOT EXISTS (SELECT Nom, IDSociete FROM stagiaire, societe WHERE Nom = 'Paul' AND IDSociete = (SELECT ID from soceite where NomSoc='Promoplantes'))
BEGIN
INSERT INTO stagiaire(Nom, Email, Telephone, IDSociete, Pseudo, Password)
VALUES('Paul', 'paul#mail.com',
'000000000', '2'
, 'paulPseudo', 'paulPassword')
END
And it wont works.
I have :
Paul who work for Promoplantes
I want :
Paul who work for KebabHouse
(this is an example)
My query just see there is already a Paul so it doesnt insert it. But they are different Paul's !
Do you have any idea where is my failure ?
Also, do you know any website which can check my queries syntax in the future ?
Give this a go. It should remove the need for to use the sub-query by replacing it with a join (more efficient)
IF NOT EXISTS (
SELECT
*
FROM stagiaire stg
LEFT JOIN societe sct
ON stg.IDSociete = sct.ID
WHERE Nom = 'Paul'
AND NomSoc = 'Promoplantes'
)
BEGIN
INSERT INTO stagiarie (Nom, Email, Telephone, IDScoiete, Pseudo, Password)
VALUES ('Paul', 'paul#mail.com', '000000000', '2', 'paulPseudo', 'paulPassword')
END
I'm trying a MySQL Insert Query with mix of static & Dynamic Values. The INSERT command is.
INSERT INTO ebdb.requestaction(RequestID,
ActionID,
TransactionID,
IsActive,
IsComplete)
VALUES (
1,
**Dynamic Value from Below Query,
Dynamic Value from Below Query,**
1,
0);
The Query to fetch the field 2 & 3 come from the below Query.
SELECT transitionaction.TransitionID, transitionaction.ActionID
FROM transitionaction
INNER JOIN transition
ON transitionaction.TransitionID = transition.TransitionID
WHERE transition.TenantID = 1
AND transition.ProcessID = 1
AND transition.CurrentStateID = 1
ORDER BY transitionaction.TransitionID;
I'm doing something wrong in here.
Please guide me as to how this can be achieved in the most optimized way.
You can select static values as part of a query, e.g.:
INSERT INTO ebdb.requestaction(RequestID, ActionID, TransactionID, IsActive, IsComplete)
SELECT 1, transitionaction.ActionID, transitionaction.TransitionID, 1, 0
FROM transitionaction
INNER JOIN transition
ON transitionaction.TransitionID = transition.TransitionID
WHERE transition.TenantID = 1
AND transition.ProcessID = 1
AND transition.CurrentStateID = 1
ORDER BY transitionaction.TransitionID;
For more information, refer to MySQL's Insert...Select Syntax
I am trying to make a query within 2 tables.
I have one table with id_person and message: (addedEvent) and other table with id_person, names and surnames. (registered)
The problem is that messages can be related to one person (id_person) or to everyone (id_person = 0) so I want to take the name and the surname of every person when needed and not not when not needed. I tried this:
SELECT message,
IF(id_person!= 0,
SELECT name
FROM registered
WHERE addedEvents.id_person= registered.id_person, 0) AS name,
IF(id_person!= 0,
SELECT surname
FROM registered
WHERE addedEvents.id_person= registered.id_person, 0) AS surname
FROM addedEvents
But gives me a syntax error and I don't see it.
Try this:
SELECT message,
IF(
id_person != 0,
(
SELECT name
FROM registered
WHERE addedEvents.id_person = registered.id_person
),
'0'
) AS name,
IF(
id_person != 0,
(
SELECT surname
FROM registered
WHERE addedEvents.id_person = registered.id_person
),
'0'
) AS surname
FROM addedEvents
You can't think of SQL as a regular programming language with flow control structures and the like. And your query does not even seem to need it:
SELECT ae.message, re.name, re.surname
FROM addedEvents ae
LEFT JOIN registered re ON ae.id_person=re.id_person
I think you are trying to select message of a related person in registered.
Try this:
select
r.name, r.surname, e.message
from
addedEvents e, registered r
where
e.id_person = r.id_person
and r.id_person != 0
I have an SQL question which may be basic to some but is confusing me.
Here is an example of column names for a table 'Person':
PersonalID, FirstName, LastName, Car, HairColour, FavDrink, FavFood
Let's say that I input the row:
121312, Rayna, Pieterson, BMW123d, Brown, NULL, NULL
Now I want to update the values for this person, but only if the new value is not null, Update:
121312, Rayna, Pieterson, NULL, Blonde, Fanta, NULL
The new row needs to be:
121312, Rayna, Pieterson, BMW123d, Blonde, Fanta, NULL
So I was thinking something along the lines of:
Update Person(PersonalID, FirstName, LastName, Car, HairColour,
FavDrink, FavFood) set Car = #Car (where #Car is not null), HairColour
= #HairColour (where #HairColour...)... etc.
My only concern is that I can't group all the conditions at the end of the query because it will require all the values to have the same condition. Can't i do something like Update HairColour if #HairColour is not Null
Id use coalesce for this:
http://msdn.microsoft.com/en-us/library/ms190349.aspx
update Person
set Car = coalesce(#Car, Car), HairColour = coalesce(#HairColour, HairColour)
The following should work:
UPDATE Person
SET Car = ISNULL(#Car, Car),
HairColour = ISNULL(#HairColour, HairColour),
...
It uses the SQL Server ISNULL function, which returns
the first value if it is non-null,
or, otherwise, the second value (which, in this case, is the current value of the row).
You can use the isnull function:
update Person
set
Car = isnull(#Car, Car),
HairColour = isnull(#HairColour, HairColour),
FavDrink = isnull(#FavDrink, FavDrink),
FavFood = isnull(#FavFood, FavFood)
where PersonalID = #PersonalID
Set the column equal to itself with an isnull round it setting it to your parameter.
UPDATE
YourTable
SET
YourColumn = ISNULL(YourColumn, #yourParameter)
WHERE
ID = #id