I have this SQL query that has been working great. I would like to have something similar that would delete a line from PRC_FIX when the column DESCR in IM_ITEM begins with Clearance instead of where ITEM_VEND_NO = 'GAMES WORK'.
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND `IM_ITEM`.`ITEM_VEND_NO` = 'GAMES WORK'
Thanks for your help.
Edit: This was marked as a possible duplicate. I don't know that looking at the suggested duplicate would have helped me because I wouldn't have known how to implement it in this scenario involving 2 tables, but I'm willing to admit that might be my fault due to me being new to SQL.
You can use
DELETE PRC_FIX
FROM PRC_FIX
INNER JOIN IM_ITEM
ON IM_ITEM.ITEM_NO = PRC_FIX.ITEM_NO
WHERE UPPER(IM_ITEM.DESCR) LIKE 'CLEARANCE%';
You need to use the wildcard %.
in order to match with this string with different string which begins with "Clearance" you need to use "Clearance%".
Look here: SQL like search string starts with
You're fixed code:
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND IM_ITEM.DESCR LIKE 'Clearance%'
DETELE FROM PRC_FIX WHERE ITEM_NO IN (SELECT ITEM_NO FROM IM_ITEM WHERE ITEM_VEND_NO` = 'GAMES WORK')
Related
I have 2 tables:
table 1:userdetails with fields:uid,mobile,name
table 2:accountdetails with fields:uid,savings,balance
Where uid is common and primary key in both tables.
Now Iam trying to get mobile,savings values from both tables where uid =1 how can we get.I tried below but didnt worked.
select mobile,savings
from userdetails,accountdetails
where userdetails.uid='1'AND
userdetails.uid = accountdetails.uid
Please can some one help
More modern version using the JOIN syntax:
SELECT
a.`mobile`,
b.`savings`
FROM `userdetails` a
JOIN `accountdetails` b
ON a.`uid` = b.`uid`
WHERE a.`uid` = 1
The query should probably look like this:
select ud.mobile, ad.savings
from userdetails ud left join
accountdetails ad
on ud.uid = ad.uid
where ud.uid = 1;
Notes:
Use proper, explicit JOIN syntax.
Use meaningful table aliases and qualify all columns names.
The left join keeps all rows, even if there is no match in the second table. That might be your problem.
I am assuming that uid is actually a number. Don't put numbers around numeric constants.
You could turn on your error display to show you exactly what line is causing the issue.
ini_set('display_errors', 1);
You can try this.
SELECT `ud`.`mobile`, `ad`.`savings`
FROM `userdetails` `ud`
INNER JOIN `accountdetails` `ad` ON `ud`.`uid` = `ad`.`uid`
WHERE `us`.`uid` = 1
Use join
select mobile,savings
from userdetails join accountdetails on userdetails.uid = accountdetails.uid
where userdetails.uid='1'
can you help me with an sql query?
I want to get a value from another table but cannot use an inner join because the "join" column is not exactly the same in these two tables. Instead I must check the place where "Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max" holds.
This is my research efford so far:
UPDATE
Table_A
SET
Table_A.clubState = Table_B.state
FROM
clubs_data AS Table_A
JOIN zip_to_state AS Table_B
WHERE
Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max
However it draws an syntax-error on line 5.
Thank you!
Here is your query re-written with a valid syntax:
UPDATE clubs_data
SET clubState = (
SELECT state
FROM zip_to_state
WHERE clubs_data.clubZipCode >= zip_to_state.zip_min AND clubZipCode <= zip_to_state.zip_max
LIMIT 1
);
I do not know what you want to achieve, but generally the update syntax using JOIN would be something like this:
UPDATE clubs_data AS Table_A
JOIN zip_to_state AS Table_B
ON Table_A.clubZipCode >= Table_B.zip_min
AND Table_A.clubZipCode <= Table_B.zip_max
SET Table_A.clubState = Table_B.state;
EDIT: The solution just proposed by #Gab using a subquery might be better suited for you in this case.
EDIT OK so the main problem here was initial column1 FROM table1 with the join. Even that column1 has to be fully defined as table1.column1 even tho it is next to the FROM which seems at best odd to me. But I guess this is a newb error and I hope other newbs will find this useful.
//========================================================================
Have used simple joins before without problems. I thought the table.column format was unambiguous.
Warning is:
Integrity constraint violation: 1052 Column 'transmissionProgramID'
in field list is ambiguous'
The SQL is:
SELECT transmissionProgramID FROM transmissionProgramOwner
JOIN transmissionProgram on transmissionProgram.transmissionProgramID
= transmissionProgramOwner.transmissionProgramID WHERE
ownerType = '$ownerType' AND ownerID = '$ownerID' ORDER BY startDate
The two table transmissionProgramOwner and transmissionProgram both have fields called transmissionProgramID. I just cannot see how the table.column leaves anything ambiguous.
Sure it is something simple but I cannot see it. And I apologize for the long variable names but helps me keep things clear.
Additional info: Both transmissionProgramID are set to unique in both tables. I have tried every flaovor of JOIN but I think a simple join is allowed which just returns all records that match... In any case have tried every type of join just to make sure.
Friend try this
SELECT t1.transmissionProgramID FROM transmissionProgramOwner t1
JOIN transmissionProgram t2 on t2.transmissionProgramID
= t1.transmissionProgramID WHERE
t1.ownerType = '$ownerType' AND t1.ownerID = '$ownerID' ORDER BY t1.startDate;
change to
SELECT transmissionProgram.transmissionProgramID FROM
transmissionProgramOwner JOIN transmissionProgram on
'transmissionProgram.transmissionProgramID'
= 'transmissionProgramOwner.transmissionProgramID' WHERE ownerType = '$ownerType' AND ownerID = '$ownerID' ORDER BY startDate
I keep getting an error saying that a specific column, doesn't exist for b.BookCode But I am fully aware that it does exist! I just wanted to make sure after staring at this for so long that I am not missing something obvious.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM (Inventory i, Author a, Book b)
WHERE (i.BookCode = b.BookCode AND b.AuthorNum = a.AuthorNum);
I'm very new to SQL so I'm unsure if my syntax is off, Also do I need parentheses around the columns that I mentioned in SELECT. I had parentheses around them at first and got an error and was confused as to why.
Thanks!
You are using the old join syntax, you should use INNER JOIN instead.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i
INNER JOIN Author a
USING (BookCode) -- You can also use ON, but USING remove the ambiguous columns
INNER JOIN Book b
USING (AuthorNum); -- Same thing here
If you get an error saying one column doesn't exist, well, you should double check it. MySQL isn't lying to you for the sake of it! You might do your query on an outdated database, for instance.
Minimally, you should change this to:
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i, Author a, Book b
WHERE i.BookCode = b.BookCode AND
b.AuthorNum = a.AuthorNum;
... and this is assuming that the columns in the SELECT part of your select statement are not ambiguous (i.e., found in more than one of the tables in the FROM part).
I realized that i was using a varchar attribute as a index/key in a query, and that is killing my query performance. I am trying to look in my precienct table and get the integer ID, and then update my record in the household table with the new int FK, placed in a new column. this is the sql i have written thus far. but i am getting a
Error 1093 You can't specify target table 'voterfile_household' for update in FROM clause, and i am not sure how to fix it.
UPDATE voterfile_household
SET
PrecID = (SELECT voterfile_precienct.ID
FROM voterfile_precienct INNER JOIN voterfile_household
WHERE voterfile_precienct.PREC_ID = voterfile_household.Precnum);
Try:
update voterfile_household h, voterfile_precienct p
set h.PrecID = p.ID
where p.PREC_ID = h.Precnum
Take a look at update reference here.
Similarly, you can use inner join syntax as well.
update voterfile_household h inner join voterfile_precienct p on (h.Precnum = p.PREC_id)
set h.PrecID = p.ID
What if the subquery returns more than one result? That's why it doesn't work.
On SQL Server you can get this type of thing to work if the subquery does "SELECT TOP 1 ...", not sure if mysql will also accept it if you add a "limit 1" to the subquery.
I also think this is pretty much a duplicate of this question ("Can I have an inner SELECT inside of an SQL UPDATE?") from earlier today.
Firstly, your index on a varchar isn't always a bad thing, if it is not a key you can shrink how much of the field you index to only index say the first 10 chars or so.
Secondly, it won't let you do this as if it is a set that is returned it could break.