I have two tables. One has a list of baseball players associated with numbers representing their position on the field, and the other table has a list of numbers associated with the names of the field positions.
Using a select statement, how do I select the position number from the players table, and have it show the position name (converting the position number to the name)?
I'm new to MySQL, so please keep it simple and informative. Thank you!
Just join the two tables on the number column.
See MYSQL JOINS HERE: https://dev.mysql.com/doc/refman/5.0/en/join.html
Here is an example, since I don't know your actual schema.
SELECT pt.player_name as PLAYERNAME, fp.position_name as POSITION
FROM playersTable AS pt
INNER JOIN fieldPositions AS fp ON fp.position_number = pt.position_number
You can try like this
SELECT pf.position_name as position, p.position_number as playerName
FROM field_positions as pf, player as p
WHERE fp.position_number = p.position_number
Related
I'm working on a mysql database select and cannot find a solution for this tricky problem.
There's one table "words" with id and names of objects (in this case possible objects in a picture).
words
ID object
house
tree
car
…
In the other table "pictures" all the information to a picture is saved. Besides to information to resolution, etc. there are especially informations on the objects in the picture. They are saved in the column objects by the ids from the table words like 1,5,122,345, etc.
Also the table pictures has a column "location", where the id of the place is written, where I took the picture.
pictures
location objectsinpicture ...
1 - 1,2,3,4
2 - 1,5,122,34
1 - 50,122,345
1 - 91,35,122,345
2 - 1,14,32
1 - 1,5,122,345
To tag new pictures of a particular place I want to become suggestions of already saved information. So I can create buttons in php to update the database instead of using a dropdown with multiple select.
What I have tried so far is the following:
SELECT words.id, words.object
FROM words, pictures
WHERE location = 2 AND FIND_IN_SET(words.id, pictures.objectsinpicture)
GROUP BY words.id
ORDER BY words.id
This nearly shows the expected values. But some information is missing. It doesn't show all the possible objects and I cannot find any reason for this.
What I want is for example all ids fo location 2 joined to the table words and to group double entries of objectsinpicture:
1,5,122,34
1,14,32
1,5,14,32,34,122
house
...
...
...
...
...
Maybe I need to use group_concat with comma separator. But this doesn't work, either. The problem seems to be where condition with the location.
I hope that anyone has an idea of solving this request.
Thanks in advance for any support!!!
This is a classic problem of denormalization causing problems.
What you need to do is store each object/picture association separately, in another table:
create table objectsinpicture (
picture_id int,
object_id int,
primary key (picture_id, object_id)
);
Instead of storing a comma-separated list, you would store one association per row in this table. It will grow to a large number of rows of course, but each row is just a pair of id's so the total size won't be too great.
Then you can query:
SELECT w.id, w.object
FROM pictures AS p
JOIN objectsinpicture AS o ON o.picture_id = p.id
JOIN words AS w ON o.object_id = w.id
WHERE p.location = 2;
I put together this example to help
http://sqlfiddle.com/#!9/51db24
The idea is I have 3 tables. One is the "root" table, in this case person which has a score attached to it. They then have some category I need to group by person_cat.cat and a one to many field called CS.
I would like to query for the average of the score, the sum of the one to many field person_co.co and group by the category.
SELECT
person_cat.cat,
person.id,
SUM(person_co.co),
AVG(person.cs)
FROM
person
LEFT JOIN person_co USING (id)
LEFT JOIN person_cat USING (id)
GROUP BY cat;
The issue I'm currently having is the average gets thrown off due to the join for the one to many. I can accomplish this with multiple queries, which is ok if that is the answer. However it would be nice to get this as one query.
I have a two tables one for news items and one for images.
News Table Structure
ID - numeric
newstitle - text
newsdesc - text
newsimages - text
Image Table Structure
ID - numeric
medianame - text
So a common news record would be:
ID = 1, newstitle="asdasfa", newsdesc="afsdgsgs", newsimages="1,2"
And the image table record would be:
ID=1, medianame="image.jpg"
I am trying to create a select statement that reads the data from the news table but provides me with the image name from the image table instead of the IDs.
If there is anything else ive left out let me know
Thanks
You can use FIND_IN_SET function
Example:
SELECT `news`.`ID`, `news`.`newstitle`, `news`.`newsdesc`,
GROUP_CONCAT(`images`.`medianame` SEPARATOR ',') AS `newsimages`
FROM `news`
LEFT JOIN `images` ON FIND_IN_SET(`images`.`ID`,`news`.`newsimages`)
GROUP BY `news`.`ID`
P.S. if you have (or planning to have) a big amount of rows, then you must to normalize the data.
it seems to be simple
SELECT N.*, I.medianame FROM News_Table N left join Image_Table I
ON N.Id=I.Id
I have assumed that tables names are News_table and Image_table respectively and that one news piece may have more than one image.
I have a table with a bunch of orders... one of the columns is order_status. The data in that column ranges from 1 to 5. Each number relates to a name, which is stored in another table that relates that number to the respective name.
SELECT order_id , order_status FROM tablename1
The above would just return the numbers 1,2,3,4,5 for order status. How can i query within the query on the fly to replace these numbers with their respective names.
Also, what's the term used to describe this. I'd Google it if i knew what the appropriate term was.
Each number relates to a name, which is stored in another table that
relates that number to the respective name.
JOIN it with the other table:
SELECT
t.order_id,
s.StatusName
FROM tablename1 AS t
INNER JOIN the statusesTable AS s ON t.order_status = s.status_id;
We are doing a database query in class. And it's using relational keys. I don't know how to get the query to run. Here is what is says.
For each movie, list its number and title, along with the number and name of the actors who appeared in it.
This is what I have, but it doesn't work
SELECT `Movie`,`Movie_ID`,`ActorNum` FROM `Movies`
Union
Select Actor.Fname, Actor.Lname FROM Actor
;
Im not sure what all the column names are but if ActroName would be the actors name would this be what you are looking for?
SELECT Movies.Movie,
Movies.Movie_ID,
Movies.ActroNum,
Actor.ActroName
FROM Movies
JOIN Actor ON
Actor.ActroNum = Movies.ActroNum