Say for example you had a CD Database, where you wanted to increase the CD prices of all CD's with the genre 'Pop' by 10%. However the type Genre is a SET. Where it can be in multiple genre's, such as RnB and Rock.
My code is as follows:
UPDATE CD
set price = price * 1.1
WHERE genre = 'Pop';
However my code is only updating rows where the Genre is ONLY pop. If the Genre is 'Rock,Pop,RnB', it is not updated. What am i doing wrong?
You need to use LIKE (Documentation):
... WHERE genre LIKE '%Pop%'
This will match if your genre is "Rock,Pop,RnB."
You should be aware that you are using a non-normalized structure. A better design would be to have a genre reference table:
CD_Genre (CD, Genre)
use like keyword
UPDATE CD set price = price * 1.1 WHERE genre like '%Pop%';
The best way to access SET values in queries is FIND_IN_SET:
UPDATE CD
set price = price * 1.1
where FIND_IN_SET('Pop', `genre`)
More information about FIND_IN_SET can be found here: http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set
Related
I am doing a query for a class where I need to select pet owner names based on whether or not they own a Chihuahua.
I have to use two tables, PetOwner and Pet. PetOwner has an owner number (ownerNo), owner name (oLName, oFName), and Pet has owner number and petType. Here is the code I am trying to use:
SELECT
PetOwner.oFName,
PetOwner.oLName
FROM
PetOwner,
Pet
WHERE
PetOwner.ownerNo = (SELECT
Pet.ownerNumber
FROM
Pet
WHERE
Pet.petType = 'Chihuahua'
);
The error says "unknown command beginning "Pet.petTyp..." - rest of line ignored." I know the issue is in the WHERE clause, but I can't seem to tweak it to where it will work, and I am sure the answer is obvious.
I just need a second set of eyes to look it over.
How can i do this ?
Inside your query:
SELECT PetOwner.oFName, PetOwner.oLName
FROM PetOwner, Pet
You have mentioned the table 'Pet' here. Table 'Pet' shouldn't be mentioned here, as you're selecting the oFName and oLName from the table 'PetOwner', not 'Pet'.
A simple join query will give you exactly what you want. No need to use subquery in the above mentioned way. You need to join Pet table with PetOwner table, based on PetOwner.ownerNo = Pet.ownerNumber, under the condition that Pet.petType is 'Chihuahua'.
So the completed query is:
SELECT po.oFname, po.oLName
FROM PetOwner as po
JOIN Pet as p
ON po.ownerNo = p.ownerNumber
WHERE p.petType = 'Chihuahua'
The following query worked for me:
SELECT ownername
FROM PetOwner
WHERE PetOwner.ownerNo = (SELECT Pet.ownerNo FROM Pet WHERE Pet.petType = 'Chihuahua');
Regards.
SELECT oFName, oLName
FROM PetOwner
WHERE ownerNo = (SELECT ownerNumber FROM Pet WHERE petType = 'Chihuahua');
Your query is confusing sql for which Pet to select parent one or child one. Provide alias for child table in your sub query as mentioned below :
SELECT
PetOwner.oFName,
PetOwner.oLName
FROM
PetOwner,
Pet
WHERE
PetOwner.ownerNo = (SELECT
p.ownerNumber
FROM
Pet p
WHERE
p.petType = 'Chihuahua'
);
What I'm trying to do its edit the information from a row adding more data, for example:
select name, obs from users where area='it'
It gives me:
name obs
charles vegetarian
xena otaku
and I want to add to the their obs 'friendly hard worker'
I have tried:
update users set obs=obs+' frienly hard worker' where area='it'
but it didn't work, the result that I want is:
name obs
charles vegetarian frienly hard worker
xena otaku frienly hard worker
In MySQL, the plus sign + is an operand for performing arithmetic operations.
You need to use the CONCAT() function to concatenate strings together.
UPDATE users
SET obs = CONCAT(obs,' frienly hard worker')
WHERE area='it';
update users set obs= CONCAT('string1', column1 , 'string2', column1 , 'string3' ) where area='it'
Hey guys how do you add two values on separate fields but on the same table
for example:
tblbooks
Quantity
Borrowed
each time a user issue a book to a borrower the Quantity its reduce by 1 and Borrowed is added by 1....
INSERT INTO tablename(field1,field2)
VALUES(v1,v2)
In your case I guess you need to update.
Update yourtable
SET Quantity =Quantity-1,
Borrowed=Borrowed+1
Where userid=1
The way I generally do it is select the row I want to update using LinQ and then just update the values.
For example:
With (From rw In tblBooks Select rw Where rw.Item("MyCondition").ToString = "Condition").First
.Item("Quantity") = .Item("Quantity") - 1
.Item("Borrowed") = .Item("Borrowed") + 1
End With
... I didn't test this code, and it doesn't take into account conversion, error checking, etc, but I hope it conveys the idea...
I have a table that stores image URLs by product id and am trying to write a query that will pull all image results onto one line. The table looks something like:
photoFlag photoName objectID
where photoName holds the relative path to the image and photoFlag identifies the type of image (full, thumb, etc). I started off by writing a quick query to get two images (with different flags) to pull up. This is what I have:
select t2.pic, t4.pic, t2.id from
(select p2.photoName as pic, p2.objectID as id from ds_photos as p2 where p2.photoFlag = 2) as t2,
(select p4.photoName as pic, p4.objectID as id from ds_photos as p4 where p4.photoFlag = 4) as t4
where t2.id=t4.id
which seems to be a correct query but when I execute via phpMyAdmin it never returns a result and shows up indefinitely (well, at least for about 40 min) in the "SHOW PROCESS" list. Is there something that I have wrong that I'm not seeing in here that is causing an endless loop?
Ideally I would like grab only pics with photoFlag=2 (some products have multiple images) and put them into the same row but I have no idea where to start with that. Any suggestions?
Thanks for the help.
Use: GROUP_CONCAT:
SELECT id, GROUP_CONCAT(photoName ) As pic
FROM ds_photos
WHERE (photoFlag = 2 or photoFlag = 4) and id = ?;
GROUP BY id
replace the ? with the ID you want.
So I have a manual in this table:
id lang header_name text
1 uk Youth Development It's very important
2 dk tst hejsa
3 uk tst hello sir
And I want to make a query that fetches all manual entries for a given language (danish in this case). If for some reason not all 100% of the original manual entries (the UK ones), has been translated I want to get the english entry instead. Is that even possible in table formats such as this?
I guess it would be something with a "group by header_name" of some sorts, but not sure.
Try this, i dont have an SQL and hence this is not tested
The tables t1, t2, t3 refer to the same table use an alias to distinguish them;
select * from t3
where t3.lang IN ('DK','UK')
and t3.ID NOT IN
(select t1.id
FROM t1,t2
where t1.header_name = t2.header_name
AND t2.lang = 'DK'
AND t1.lang = 'UK'
)
Essentially first you need to find the ID that have translation, and then exclude them.
This might do the trick but it is not optimized:
SELECT *
FROM the_table
WHERE lang = 'dk'
UNION
SELECT *
FROM the_table
WHERE lang <> 'dk' AND header_name NOT IN (
SELECT header_name
FROM the_table
WHERE lang = 'dk'
)
I cant comment but all i want to ask is:
In the example you just put up, the rows with Id 2 and ID 3 are the same entries only different language?
Id say you pull it out and make two tables
Example
id
sort
(all other generic columns)
example_translations
id
example_id
language_id
header_name
text
Then if querying for the danish translation of an example with id 1 it'll return the example_translations row of this entity. if it returns nothing you can query for the english version.
I dont think it is possible to do something like this on Mysql level
The way i understand this, you want to get the english content if the danish content is missing?.. You might want to add a column to your table where you mark your entries. (dont know if your "header_name" column does that efectly for you, i'm guessing that as well will be translated?..
Anyway, a column named "entry_id" where "tst dk" and "tst uk" would both have id "2" for an example, you should then when you load you manual ask for the "entry_id" and first look for the dk entry, and if it's not there, load the uk entry.