I am building a query and I need to select from the log table multiple columns, my issue is that i'm trying to find a way to select a column that has an FK in a table that has an FK to another table.
I have:
log.number_id,
numbers.number_id
numbers.country_id,
countries.country_id
Query is almost done, my only issue is that I need to show countries.country_id through an intermediary table FK numbers.country_id, I believe this is an INNER JOIN yet I have no idea how to create the concatenation, I searched google for this, yet I couldn't find something like a general formula of how to execute such an intermediary join.
I'm guessing you're looking for something like this.
Basically joining the table with both id's to the other tables on the common id.
SELECT log.*, ctry.*
FROM numbers AS ctrylog
JOIN log
ON log.number_id = ctrylog.number_id
JOIN countries AS ctry
ON ctry.country_id = ctrylog.country_id
Related
I'm making a simple blog as a learning excercise. Here are two tables I have but simplified.
Table Users contains columns "id" and "url"
Table Entries contains column "user_id"
I have the url of the user and I want to get all the entries for that user. I know how I can do this with two mysql calls. First I would use the url to get the id, then in another call use the id to get the entries.
I know how I could join the tables once I have the id. But I don't know how I can join the tables with the url before I have the id.
This is probably pretty obvious, but I've been reading up on table joins all day and not seen an example that covers what I'm trying to do, and not sure how to word it to get results from google.
Actually. I think I might have figured it out now. This seems to work.
SELECT users.id,
users.username,
entries.title,
entries.content,
entries.date
FROM users
INNER JOIN entries
ON users.id = entries.user_id
WHERE users.url = 'dreamofsleeping'
So my problem is as follow, I have a table in MySQL with a UserId column and an ObjectId column (its a many to many relationship), and what I would like is to have is a query that gives me the list of objects that user X and Y share. Not sure how to make the joins to make this happen.
Use query something like below using self join
Select columns from table t1 join table t2 on t1.objectid=t2.objectid where t1.userid=X and t2.userid=Y
I have three tables 'users', 'friends', 'friendsrequests'.
'Users' contains an id, a firstname and a lastname, 'friends' and 'friendsrequests' both contain users_id_a and users_id_b.
When I search for new friends, I select id's where firstname is LIKE :whatever or lastname LIKE :whatever. However, I want to exclude those id's which are present in the other two tables.
I know how to solve this via application logic, but I also know I shouldn't do this. I know I shouldn't chain the SELECT statements and that I should use joins.
You've answered your own question in that you know you can use joins. There are plenty of examples available here on how to do a join in MySQL.
There are several join types but the one you require in this instance is probably a LEFT OUTER. You could do a then do the filtering on the field on the other two tables by using a IS NULL. So what this is doing is joining on the additional tables regardless if there is any data in those tables. Using a WHERE IS NULL to filter out those that are present.
Rather than using joins you could take a WHERE NOT EXISTS approach. This logic might be more up your street if you're not familiar with SQL joins.
An example might be:
SELECT * FROM FRIENDS f
WHERE NOT EXISTS (SELECT 1 FROM friendsrequests fr WHERE f.user_id = fr.user_id)
Some examples can be found here:
SELECT * WHERE NOT EXISTS
Another approach in using the IN statement or specifically the WHERE NOT IN (SELECT ...)
Hopefully this will guide you if you're still stuck post your exact sql schema and the requirement on a site like http://sqlfiddle.com/ and you'll more likely get more specific response.
I have two tables with the following descriptions:
record(record_id,min_index,max_index)
points(point_id,point_name,val1,val2,rank), point_id being on auto-increment
The min_index,max_index from record table points to the point_ids in the point table.
i.e. for a particular record, the respective points are >=min_index and <=max_index.
I need to merge these two tables such that, the final table resembles something like this
points(point_id,record_id,point_name,val1,val2,rank)
I know this is against the normalisation criteria, but this seems to work very well with the problem i have. I'm not sure on how to go about merging these two tables.
Just join the tables using MySQL's BETWEEN ... AND ... operator to specify your join criterion:
SELECT points.point_id,
record.record_id,
points.point_name,
points.val1,
points.val2,
points.rank
FROM points JOIN record ON
points.point_id BETWEEN record.min_index AND record.max_index
SELECT a.point_id,
b.record_id,
a.point_name,
a.val1,
a.val2,
a.rank
from points a , record b
where ((point_id>=min_index)and (point_id<=max_index))
I'm trying to query my database for unused listId's but can't quite seem to get the query syntax correct. Here is what I've tried:
SELECT DISTINCT pkListId FROM tblList
INNER JOIN InUseLists ON
tblList.pkListId NOT LIKE InUseLists.fkListId
Where tblList holds all List data, and InUseLists is a view holding all distinct listId's from my relational table.
Currently my tblList holds listId 1-9
my relational table has fkListId 1,8,9 (used more than once)
So my view gets distinct values from the relational table - 1,8,9.
What is wrong with my query syntax?
It sounds like you want a left join (which can succeed whether or not it finds a match in the "right" table), and then to select those rows where the join, in fact, failed to work.
Something like:
SELECT DISTINCT pkListId FROM tblList
LEFT JOIN InUseLists ON
tblList.pkListId LIKE InUseLists.fkListId
WHERE InUseLists.fkListId is null
(If LIKE is the right operator here. Most joins use a simple equality check)
If this still isn't right, it might be better if you edited your question to include the sample data and expected results in a tabular form. At the moment, I'm still not sure what the contents of your tables are, and what you're aiming for.
Try:
SELECT DISTINCT tblList.pkListId, FROM tblList, InUseLists WHERE
tblList.pkListId NOT IN(InUseLists.fkListId)