populating field in access 2007 based on another field - sql

I have data for tennis. I have two tables,(game_atp(Player 1, player 2, Name_T, surface) and tours_atp(ID, tournament, court surface)). In games_atp table i want to create a field surface and put the surface based on the tournament its played in( so based on Name_T), getting the info from tours_atp table.
thank you

Do you have a way to connect a row in game_atp to a row in tours_atp? For instance, is tours_atp.ID the same as game_atp.Name_T? There has to be a way to connect them. Also am I assuming that surface will be equal to whatever is in court surface? Assuming that tours_atp.ID is the same as game_atp.Name_T then you can just do...
UPDATE game_atp Set surface = (SELECT court_surface FROM tours_atp WHERE tours_atp.ID = game_atp.NAME_T)
This code will update all rows. If you want to update only a specific game then you will have to use a WHERE clause and tell it the id of the game like so...
UPDATE game_atp Set surface = (SELECT court_surface FROM tours_atp WHERE tours_atp.ID = game_atp.NAME_T ) WHERE game_atp.Name_T = '5555'

Related

Trouble Updating Field on a Particular Row

I am trying to update a record in a particular existing row of data.
EX: Existing Output; Team, Year, Player, Position
LA Lakers 2016 Jim Bowen
Can someone provide me a sample of the code to update 'Position' column
Update LAL_Player_Stats
Set POSITION = C
Where Player = 'Bowen'
This returns: (0 row(s) affected)
You may have to try Player = 'Jim Bowen'.
Position should not be all caps.
Also, you need single-quotes on the "C": Set Position = 'C'.
Making your final statement:
Update LAL_Plater_Stats
Set Position = 'C'
Where Player = 'Jim Bowen'
Update LAL_Player_Stats SET position = 'C' WHERE player = 'Jim Bowen'
In the future, why not provide us the whole table so we can determine what you're missing when you update a row. It might help us more. :)

Selecting rows from Parent Table only if multiple rows in Child Table match

Im building a code that learns tic tac toe, by saving info in a database.
I have two tables, Games(ID,Winner) and Turns(ID,Turn,GameID,Place,Shape).
I want to find parent by multiple child infos.
For Example:
SELECT GameID FROM Turns WHERE
GameID IN (WHEN Turn = 1 THEN Place = 1) AND GameID IN (WHEN Turn = 2 THEN Place = 4);
Is something like this possible?
Im using ms-access.
Turm - Game turn GameID - Game ID Place - Place on matrix
1=top right, 9=bottom left Shape - X or circle
Thanks in advance
This very simple query will do the trick in a single scan, and doesn't require you to violate First Normal Form by storing multiple values in a string (shudder).
SELECT T.GameID
FROM Turns AS T
WHERE
(T.Turn = 1 AND T.Place = 1)
OR (T.Turn = 2 AND T.Place = 4)
GROUP BY T.GameID
HAVING Count(*) = 2;
There is no need to join to determine this information, as is suggested by other answers.
Please use proper database design principles in your database, and don't violate First Normal Form by storing multiple values together in a single string!
The general solution to your problem can be accomplished by using a sub-query that contains a self-join between two instances of the Turns table:
SELECT * FROM Games
WHERE GameID IN
(
SELECT Turns1.GameID
FROM Turns AS Turns1
INNER JOIN Turns AS Turns2
ON Turns1.GameID = Turns2.GameID
WHERE (
(Turns1.Turn=1 AND Turns1.Place = 1)
AND
(Turns2.Turn=2 AND Turns2.Place = 4))
);
The Self Join between Turns (aliased Turns1 and Turns2) is key, because if you just try to apply both sets of conditions at once like this:
WHERE (
(Turns.Turn=1 AND Turns.Place = 1)
AND
(Turns.Turn=2 AND Turns.Place = 4))
you will never get any rows back. This is because in your table there is no way for an individual row to satisfy both conditions at the same time.
My experience using Access is that to do a complex query like this you have to use the SQL View and type the query in on your own, rather than use the Query Designer. It may be possible to do in the Designer, but it's always been far easier for me to write the code myself.
select GameID from Games g where exists (select * from turns t where
t.gameid = g.gameId and ((turn =1 and place = 1) or (turn =2 and place =5)))
This will select all the games that have atleast one turn with the coresponding criteria.
More info on exist:
http://www.techonthenet.com/sql/exists.php
I bypassed this problem by adding a column which holds the turns as a string example : "154728" and i search for it instead. I think this solution is also less demanding on the database

Using SQL to Copy specific content based on conditions of other colums

I need to use either SQL or Excel to copy the picture columns data of the other corresponding picture values for the SAME model and color when the field is NULL.
For this picture shown I need D-2 and D-4 to also say A-2.jpg instead of NULL
(It's the same A model and the color is red so copy the existing A model and red picture that's there). I need D-7 to either copy D-5 or D-6's picture value (A-4.jpg or A-5.jpg would work). So on.... If there are not particular pictures for that group (ie. model B and Black) then it can be left as NULL.
I'm trying to use group by functions and nested selects, but I am getting nowhere with this.
If you are using MS SQL Server, you can use a self join to update the table.
update r set r.picture = l.picture
from Item l
join Item r on l.model = r.model and l.color=r.color
where
l.picture is not null and
r.picture is null
Assuming your table is called "products" you might be able to do something like this:
UPDATE products p SET picture = (
SELECT picture
FROM products p2
WHERE p2.model = p.model
AND p2.color = p.model
)
WHERE p.picture IS NULL
The rules about update commands vary between different Database systems. Let us know which database you are using if the above query does not work.

SQL Query to show all available rooms under a property

My SQL knowledge is not my strongest and I am struggling to find out how I can achieve what I am looking for.
When I run my query I want to show the name of the house and then underneath I would like to display all rooms which pertain to that house (these rooms can be rented)
Desired Display
15 Property Way - PP34PQ
Double bedroom - £500.00P/M
Single bedroom - £300.00P/M
Current Display
15 Property Way - PP34PQ Double bedroom - £500.00P/M
15 Property Way - PP34PQSingle bedroom - £300.00P/M
I am guessing I need to write some sort of grouping query to do this, but I am not quite sure of how I can achieve this. My current query looks like this
SELECT properties.prop_details, properties.num_rooms, addresses.addr_line_1,
addresses.addr_postcode, addresses.addr_city, room_details
FROM rooms
JOIN properties ON properties.property_id = rooms.property_id
JOIN addresses ON addresses.addr_id = properties.prop_addr
JOIN cities ON addresses.addr_city = cities.city_id
WHERE rooms.property_id = 1;
Any help would be greatly appreciated.
it sounds like you try to build a Report and try to do the display in SQL instead of your web solution.
Keep the data and its presentation separate.
Get your datatable, and then loop through it with PHP, creating a table for every building.
Ordinarely, you would use recursion, but MySQL doesn't support it.
You can use
ORDER BY premise.name, premise.id, room.nr, room.id
My guess is you need to group by room and property fields, using the max aggregate function for address and city fields, because a property (building) can have multiple addresses, one for each entrance...
SELECT
premises.field_1
,premises.field_2
,premises.field_3
,room.field_1
,room.field_2
,room.field_3
,max(address.field1) as adr_f1
,max(address.field2) as adr_f2
,max(address.field3) as adr_f3
FROM Whatever
JOIN WHATEVER
WHERE (1=1)
AND (whatever)
GROUP BY
premises.field_1
,premises.field_2
,premises.field_3
,room.field_1
,room.field_2
,room.field_3
HAVING (WHATEVER)
ORDER BY premises.field_x, room.field_y

UPDATE SQL based on 2 conditions

Given the following table structure
Locations
LocationName|Easting|Northing
Incidents
LocationString|Easting|Northing|LocationName
LocationString is a badly formatted Subway Station Name that the user of the application can type any old rubbish in to. The eastings and Northings (Co-ordinates) are consistent however. Using them i can give the location a consistent name by looking those values up in a look up table.
In ACCESS SQL i would do the following
UPDATE INCIDENTS, Locations
SET Incidents.LocationName = Locations.LocationsName
WHERE Incidents.Easting = Locations.Easting
AND
Incidents.Northing = Locations.Northing
How do i accomplish the same in T-SQL?
UPDATE I
SET I.LocationName = L.LocationsName
FROM Incidents I
JOIN Locations L
ON I.Easting = L.Easting AND I.Northing = L.Northing