MySQL update table to fill nulls from another table - mysql

I have a table codes with fields id, code, issuedto, issuedtime which is pre-filled with items in code but which has many rows with NULLs in issuedto, issuedtime. I have another table entrants which has fields id, status.
I want to set codes.issuedto = winners.id for each row of winners with status = 'won'.
The problem is I don't have anything to join the tables on -- so I end up with a cross join and that's not what I want at all. What I really want is an inner join -- but without anything to join on. Anyone have any ideas?
EDIT: if I were doing this outside of SQL (which I might have to do?) the pseudocode would look something like:
rows = query("SELECT id FROM winners WHERE status='won'");
foreach (rows as r) {
query("UPDATE codes SET issusedto=" + r.id + ", issued=NOW() WHERE issuedto IS NULL LIMIT 1");
}

OP here. After some extensive searching and experimentation, I have come to the conclusion this cannot be done in SQL. I wrote a short script to do this work for me, similar to the pseudocode listed in the question. ACID compliance is achieved by transactions (check your DBMS for details if this is important).

Related

MySQL statement to read data from one table with checks on another table

I have these two tables:
Achievement:
Achieves:
Question:
I want to retrieve rows from table Achievement. But, I do not want all the rows, I want the rows that a specific Steam ID has acquired. Let's take STEAM_0:0:46481449 for example, I want to check first the list of IDs that STEAM_0:0:46481449 has acquired (4th column in Achieves table states whether achievement is acquired or not) and then read only those achievements.
I hope that made sense, if not let me know so I can explain a little better.
I know how to do this with two MySQL statements, but can this be done with a single MySQL statement? That would be awesome if so please tell me :D
EDIT: I will add the two queries below
SELECT * FROM Achieves WHERE Achieves.SteamID = 'STEAM_0:0:46481449' AND Achieves.Acquired = 1;
Then after that I do the following query
SELECT * FROM Achievement;
And then through PHP I would check the IDs that I should take and output those. That's why I wanted to get the same result in 1 query since it's more readable and easier.
In sql left join, applying conditions on second table will filter the result when join conditions doesn't matter:
Select * from achievement
left join achieves on (achievement.id=achieves.id)
where achieves.acquired=1 and achieves.SteamID = 'STEAM_0:0:46481449'
Besides,I suggest not using ID in the achieves table as the shared key between two tables. Name it something else.
I don't think a left join makes sense here. There is no case where you don't want to see the Achievement table.
Something like this
SELECT *
FROM Achieves A
JOIN Achievement B on A.ID = B.ID
WHERE A.SteamID = 'STEAM_0:0:46481449'
AND A.Acquired = 1;

How can I refer to a field being referred by the SELECT query?

I have 2 tables, Ongoing_Portfolio and Ongoing_Fees. Ongoing fees contains a foreign key for Ongoing-Portfolio. Below are the tables
Please have a look at the below code
SELECT Ongoing_Portfolio.*,
Ongoing_Fees.Ongoing_Net_Income FROM Ongoing_Portfolio
INNER JOIN Ongoing_Fees ON Ongoing_Fees.idPortfolio = Ongoing_Portfolio.idPortfolio
WHERE Ongoing_Portfolio.idPortfolio = 1
AND
Ongoing_Portfolio.idOngoing_Portfolio = ?
Can you see the ? mark there in the last row? What I wanted to do there is to refer the idOngoing_Portfolio field of the Ongoing_Fees which is being refereed by the query at the moment. In more detail, I need something like below.
AND
Ongoing_Portfolio.idOngoing_Portfolio = idOngoing_Fees of the Ongoing_Fees table where the query is currently accessing
How can I do this in mysql?
The direct answer to your question is to put the column in as you described it:
SELECT op.*,
Ongoing_Fees.Ongoing_Net_Income
FROM Ongoing_Portfolio op INNER JOIN
Ongoing_Fees onf
ON onf.idPortfolio = op.idPortfolio
WHERE op.idPortfolio = 1 AND
op.idOngoing_Portfolio = onf.idOngoing_Fees;
Does this do what you want? If not, you might consider asking another question. Putting the actual create table statements is more useful than a picture of two tables. Sample data and desired results are a big help both for you to understand what you want to do and to convey this information to others.

Need one chart with data not compared to each other

Ok - I'm rewording my question in hopes of getting as response. I (with help from a co-worker) have created the following SQL query that pulls the EXACT results that I need to appear in an SSRS chart:
select
(SELECT pfsp.SavingsGoal
FROM Projects AS p INNER JOIN
Projects_PerformanceServicesProject AS pfsp ON p.Id = pfsp.Id INNER JOIN
ProjectSavingsGoalTypes AS gt ON pfsp.ProjectSavingsGoalType_Id = gt.Id
WHERE (p.Id = #Project_ID)) as SavingsGoal,
(SELECT
Sum(identifiedSum)
FROM #Yaks where UPPER(name) = 'DECLINED'
GROUP BY name)as IdentifiedDeclined,
(SELECT
Sum(identifiedSum)
FROM #Yaks) as identifiedTotal,
(SELECT
Sum(implementableSum)
FROM #Yaks where upper(name) = 'APPROVED'
GROUP BY name) as implementableSavingsApproved,
(SELECT
Sum(implementedSum)
FROM #Yaks
) as implementedSavingsTotal
What the chart should ultimately look like (generally speaking):
http://i1365.photobucket.com/albums/r745/twarden11/chart_mockup_zps22cfdbf3.png
Telling you everything I've tried would take all my characters, and would be good for a laugh, and that's about it. It was also be futile, as I am an extreme novice (this is my first time to build a chart - ever, please be clear and speak in non-technical terms when possible), and my efforts I can assure had nothing to do with what I need to be trying.
So what I need are plain instructions on how to turn this query into the table graphic that I've included. I can't express how desperate I am at this point. My co-worker said it would be easier to simply pull the exact data that I need in the query, but never told me how to convert the query to a chart.
Thanks so much.
I would redesign the SQL query to return 2 columns and 5 rows. The 1st column would describe the category e.g. Goal, Identified etc. The 2nd column would present the $ values.
This would probably require a series of SELECT ... UNION ALL ... clauses, one for each of the 5 rows required.
Then I would add the 1st column to the chart as the Category Group, and the 2nd column as the Values (series).

SQL query to select based on many-to-many relationship

This is really a two-part question, but in order not to mix things up, I'll divide into two actual questions. This one is about creating the correct SQL statement for selecting a row based on values in a many-to-many related table:
Now, the question is: what is the absolute simplest way of getting all resources where e.g metadata.category = subject AND where that category's corresponding metadata.value ='introduction'?
I'm sure this could be done in a lot of different ways, but I'm a novice in SQL, so please provide the simplest way possible... (If you could describe briefly what the statement means in plain English that would be great too. I have looked at introductions to SQL, but none of those I have found (for beginners) go into these many-to-many selections.)
The easiest way is to use the EXISTS clause. I'm more familiar with MSSQL but this should be close
SELECT *
FROM resources r
WHERE EXISTS (
SELECT *
FROM metadata_resources mr
INNER JOIN metadata m ON (mr.metadata_id = m.id)
WHERE mr.resource_id = r.id AND m.category = 'subject' AND m.value = 'introduction'
)
Translated into english it's 'return me all records where this subquery returns one or more rows, without returning the data for those rows'. This sub query is correlated to the outer query by the predicate mr.resource_id = r.id which uses the outer row as the predicate value.
I'm sure you can google around for more examples of the EXIST statement

MySQL Update, Set with Where not working?

I have read other posts and I really don't understand what I am doing wrong here because it is such a simple statement.
anything in '()' are comments
Query:
[UPDATE users, sites
SET users.idSiteRegUsers='1'
WHERE sites.SiteActivateSites='DEL' ]
(TBL's to select to update within the whole query)
(Setting the users tbl idSiteRegUsers to 1)
(Where only the sites in tbl sites = DEL)
I've also read http://bugs.mysql.com/bug.php?id=52651 and tried changing the INT to VARCHAR and UPDATING 0 to DEL due to the bug but still same result.
Issue:
I have 2129 records that need updating as found using a simple select statement to understand the number of results.
([SELECT
sites.SiteActivateSites,
sites.id_Sites,
users.idSiteRegUsers,
users.CompanyNameUsers,
sites.LinkNameSites
FROM
users
INNER JOIN sites ON users.idSiteRegUsers = sites.id_Sites
WHERE
sites.SiteActivateSites != '1']) 'simple'
But the UPDATE query updates all 4000+ records, not just the records that = DEL but the ones that are reference to another value e.g. = 1.
Have I missed something?
Cheers,
Joe
Just as with your SELECT command, you need to tell MySQL how the tables are joined: your UPDATE is currently doing a full cartesian product of both tables (with every row in users joined with every row in sites - therefore the WHERE condition filtering on sites is still resulting in a match on every record from users).
Try instead:
UPDATE users JOIN sites ON users.idSiteRegUsers = sites.id_Sites
SET users.idSiteRegUsers='1'
WHERE sites.SiteActivateSites='DEL'