I have a question regarding pulling data from different tables in MYSQL. I'm sorry if this question has already been answered elsewhere, but I just can't seem to make sense of it.
I want to pull out the categories that are bound to the user's chosen newspapers.
I've got a table that tracks the user's chosen newspapers with user_id and newspaper_id.
A table with the newspapers and another table with the categories.
And at last a table which has both the newspaper_id and the category_id.
Is this possible to do with a single query? I'm really no good with sub select queries.
Thanks in advance!
Joining is probably the way to go:
SELECT c.*
FROM categories c
JOIN newspaper_categories nc ON c.category_id = nc.categroy_id
JOIN newspaper_users nu ON nu.newspaper_id = nc.newspaper_id
WHERE nu.user_id = <some id>
Related
I have a mysql database with multiple tables that describe playlists and users(each of these tables has an ID and Name for the playlist/user).
Now, i have one table "PlaySent" that has only IDs that point to one playlist and two users (a recipient and a sender).
My objective is to Select all records from this table (PlaySent) where i don't get these IDs but the names they represent.
I believe i have to use an INNER JOIN?
EDIT: I have made a sketch of the tables in question, so they can be in english
So again, i need a query that returns me all data from PlaylistSent where it doesn't show me the id, but the names of the playlist and the users in each row
Answer
Your Query will look like this, you have not posted the exact name of the fields so i am just assuming.
SELECT pl.PlaylistName as playlist_name,sender.Username as sender_name,receiver.Username as receiver_name
FROM PlaylistSent AS ps
INNER JOIN Users AS sender
ON ps.1stUserID=sender.id
INNER JOIN Users AS receiver
ON ps.2ndUserID=receiver.id
INNER JOIN Playlist AS pl
ON ps.PlaylistId=pl.id
Off topic
I would suggest you next time to:
1) include the exact database structure
2) include any SQL you tried and did not work and the output of the SQL
3) include a small sample of data
Also it would be helpfull for you to start by reading a book or a tutorial to learn the syntax and get a deeper understanding on the topic you try to learn
I am developing a personal proyect for academic books. I have some tables with +30.000 rows each for works, editions, authors and so on. All the information of the books —genres, subjects, authors, publishers, etc— is spread over a lot of tables with different types of relations.
I have a query for the main page that works, but the site takes six seconds to load. A lot of time… I was wondering which would be the proper approach for obtaining all the data I need with temporary tables.
What I want to do now is to join the temporary table _work with the related data of another table, say «genre». But the relationship between «work» and «genre» is done with the temporary table «work_has_genre».I know how to do that with normal tables in a single query:
SELECT *
FROM work a
LEFT JOIN (
SELECT GROUP_CONCAT(f_a.id SEPARATOR '|') AS genre_id, GROUP_CONCAT(f_a.genre SEPARATOR '|') AS genre_name, f_b.work_id AS _work_id
FROM genre f_a
INNER JOIN (
SELECT *
FROM work_has_genre f_b_a
) f_b
ON f_a.id=f_b.genre_id
GROUP BY f_b.work_id
) f
ON a.id=f._work_id
WHERE a.id=13
I suppose the idea would be to break this actions in parts, but I don't know how. Could someone help me with a bit of pseudocode? Or maybe this is not the best approach. Any idea will be very welcomed!
A.
As I said in comments, I would first suggest reworking/flattening the subqueries as much as possible first, but once you get to semi-independent aggregations temp tables can be helpful.
Generally, the pattern is to put each such aggregation subquery's results into it's own temp table (with an index on the field the subquery was joined to the main query on) even if that means adding tables (and the main query's WHERE) to the original subquery, and then joining to the temp table in the main query.
Just looking for some help with this, i'm sure it is incredibly simple but after so many hours doing other areas of my site, i'm going a bit batty.
I just have a gaming competition whereby I have a table called 'leaders' that has only these columns:
fk_memberid | points_total
Quite simple. Then I have this query I found elsewhere on this forum to just get the rankings of each member.
SELECT
fk_memberid,
points_total,
(SELECT COUNT(*)+1 FROM leaders WHERE points_total>x.points_total) AS rank_upper,
(SELECT COUNT(*) FROM leaders WHERE points_total>=x.points_total) AS rank_lower
FROM
`leaders` x
My question is, how do I link the fk_memberid column to another table called "members" to the corresponding column "k_memberid"? I do this all the time of course but for some reason i'm struggling in this case due to the different type of query i'm familiar with above.
Sorry for the likely incredibly easy answer. Appreciate the help.
A quick example here:
SELECT l.fk_memberid, l.points_total, m.first_name FROM leaders l
left join member m
on m.k_member_id=l.fk_member_id
WHERE ...
This will give you back a table with 3 columns, 2 from leader table and "first_name" (assuming it exists) from member table
Do one thing
SELECT * FROM Table1 T1, Table2 T2
WHERE T1.column_name = T2.column_name AND Another_comditio(if you want);
may be it will help you
SELECT *
FROM leaders
LEFT JOIN members ON members.k_memberid = leaders.fk_memberid
I'm new here and I hope I am asking my question correctly:
I am trying to implement search on forums database.
I have 'questions' table and 'answers' table (they both related by 'id_question' field). I also have 'suggestions' table and 'comments' table that are also related.
In addition I have 'profiles' table which related to every table I've mentioned before by 'profile_id' field, this profiles table holds the information on the users.
I would like to implement wide search on specific fields ('topic', 'description'...) in all the four tables I've mentioned before, and display those fields and the information of the user who wrote the post.
I really messed with this.
right now I have 4 queries (for each table), and each query makes join with profiles table.
Do you know better way to do this?
Thanks!
You can do this with INNER JOINS. I recommend starting with some simple INNER JOINS and building up your queries to be more inclusive and join more tables. It all depends on what you're trying to get, really. Be as precise as possible in returning exactly the information you want.
Here is a simple example and the assumptions I made:
Assuming the following
table: primarykey*, foreignkey#, othercolumns
questions: id_questions*, topic, description, profile_id#
answers: id_answers*, id_questions#, topic, description, profile_id#
suggestions: id_suggestions*, topic, description, profile_id#
comments: id_comments*, topic, description, profile_id#
user: profile_id*, name, details
KEYWORD: replace with your keyword or phrase
Look for a keyword in the questions and answers
SELECT * FROM questions q
INNER JOIN answers a
ON q.profile_id = a.profile_id
WHERE topic LIKE '%KEYWORD%'
OR description LIKE '%KEYWORD%';
Get the user profile for a user who used keyword in the questions and answers tables:
SELECT u.* FROM users u
INNER JOIN answers a
ON u.profile_id = a.profile_id
INNER JOIN questions q
ON q.profile_id = u.profile_id
WHERE topic LIKE '%KEYWORD%'
OR description LIKE '%KEYWORD%';
Hope this helps.
Edit: formatting.
Yes, do it in four queries.
Otherwise you will get a Cartesian product as you try to join the tables.
I think you can try using UNION. Is not it?
I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields