I'm trying to join two tables, but inputs are not the same.
On table Category category_id is an integer.
But on table url_alias category is a string like category_id=15.
I've tried:
SELECT * FROM category c
LEFT JOIN url_alias ua ON ( ua.category = 'category_id=c.category_id')
No luck. How can I reach that table?
You have placed c.catagory_id inside the quotes ('), thus making it part of the string literal. Instead, you should concat its value to category_id= literal:
SELECT *
FROM category c
LEFT JOIN url_alias ua ON ua.category = CONCAT('category_id=', c.category_id)
Related
I want to select multiple fields from table p.
The second line of this code is wrong. how to write?
except p.*
I don't want p.*
SELECT
p.id, title, price
c.`title` as `CategoryTitle`
from `tbl_products` p
INNER JOIN `tbl_categories` c
ON p.`category_FK` = c.`id`
Well, you might have your own reason so, perhaps you can do something like this:
SELECT
id, title, price, CategoryTitle
FROM `tbl_products` p
INNER JOIN
(SELECT `title` AS 'CategoryTitle', id AS 'CategoryID'
FROM `tbl_categories`) c
ON category_FK = CategoryID
Make one of the table as a subquery and define column alias that's not a duplicate with the other table. In your example, it seems like both of your tables have columns with similar names like id & title. Once you define those similar column names in the subquery with different alias, then you won't need to do p.xx or c.xx.
I have three tables called Notes another table called Tags and third as a Join table called NoteTagsJoin, Join table holds two foreign keys primary Note id and Primary Tag id. I use this query to get all Notes with tagId:
SELECT * FROM notes INNER JOIN note_tag_join ON notes.entryId = note_tag_join.noteId WHERE note_tag_join.tagId =:tagId
And this query to get all Tags:
SELECT * FROM tags INNER JOIN note_tag_join ON tags.tagId = note_tag_join.tagId WHERE note_tag_join.noteId =:noteId
How can I get Note and all its tags using just Note id with one query?
Are you looking for two joins?
SELECT n.*, t.*
FROM notes n INNER JOIN
note_tag_join nt
ON n.entryId = nt.noteId INNER JOIN
tag t
ON t.tagId = nt.tagId
WHERE n..entryId = :noteId
SELECT * FROM table_name
LEFT JOIN table_name2 ON table_name.id = table_name2.id
LEFT JOIN table_name3 ON table_name2.id = table_name3.id
WHERE table_name.id = id;
Change the "id" with the appropriate id's that you're using. It's important that the id's in the JOINs are coherent, else there will be no link between them.
If you want to select fields of the 3 tables, do this:
SELECT (fields that you want to show) FROM tableA
INNER JOIN tableB ON tableA.commonField = tableB.commonField
INNER JOIN tableC ON tableB.commonField = tableC.commonField
I have three tables.
users - user_info - districts
And I built a Inner join to get the user_id and the user_info.
Select * from users a inner join user_info b on a.id = b.user_id
But i have a column called location, inside the user_info, which returns the ID from a specific location. Just like this:
00;11
And to get the location, I have to Inner Join the user_info table, to another table called districts, because the two last characters got the ID from the district.
Thats why I would like to Inner Join all three tables, like this:
Select * from users a inner join user_info b on a.id = b.user_id inner join districts c on b.location = c.District_id
The problem is that, i want to get only the two last characters from the Location column. But i'm getting
00;11 //I would like to only get the 11
I will output everything later, using Json, and I would like to get the User info, and his location.
Is it possible to "substring" a column in SQL?
Thanks.
You could use SUBSTRING_INDEX in MySQL to get the string after the semi-colon
SELECT SUBSTRING_INDEX('00;11', ';', 2);
Or in your case:
Select SUBSTRING_INDEX(Location, ';', 2),* from users a inner join user_info b on a.id = b.user_id inner join districts c on b.location = c.District_id
Alternatively you can take the last two characters of your string using RIGHT
SELECT RIGHT('00;11', 2);
Yes, it is. See: MySQL String Functions
mysql> SELECT RIGHT('foobarbar', 4);
-> 'rbar'
For further information, please see:
SUBSTR
RIGHT
Having 5 tables
Table a_dates = id,
Table b_types = id, a_date_id, c_type_id,
Table c_types = id, name,
Table d_profiles = id, name, profile_type
Table e_ps = id, a_date_id, d_profile_id
From a_dates Need to get b_types,...then from b_types needs c_types name,... Then compare c_types name with d_profiles name and get d_profiles id.... if equals then create a records in e_ps with a_date_id, d_profile_id.
Could any one please help me in getting the query from inner join.
I tried like, it is incomplete query
INSERT INTO e_ps(id,a_date_id,a_date_type,d_profile_id,c_id)
SELECT '',a.id,'A',dp.id,'67' FROM d_profiles dp
INNER JOIN a_dates a ON {HERE I NEED NAME MATCHING WITH c_types name} = dp.name and dp.profile_type = 'A'
INNER JOIN a_dates ON a.id = a_dates.id
LEFT OUTER JOIN e_ps eps ON eps.a_date_type = 'A' AND eps.a_date_id = a_dates.id
WHERE eps.a_date_id IS NULL
This seems to be a relatively simple JOIN:-
INSERT INTO e_ps(id, a_date_id, d_profile_id)
SELECT NULL, a_dates.id, d_profiles.id
FROM a_dates
INNER JOIN b_types ON a_dates.id = b_types.a_date_id
INNER JOIN c_types ON b.c_type_id = c.id
INNER JOIN d_profiles ON c_types.name = d_profiles.name
With joins there are several types, and I suspect you are getting confused. Briefly:-
With an INNER JOIN it looks for a match that is on BOTH tables. If no
match the no record is returned.
With a LEFT OUTER JOIN it takes a record from the table on the left
and looks for a match on the table on the right. If a match great,
but if not then it still brings a row back but the columns from the
table on the right just have values of NULL.
A RIGHT OUTER JOIN is very much the same, just with the tables
reversed (most people including me avoid using this as it has no
advantages most of the time but just makes things confusing).
With a FULL OUTER JOIN it gets the records from both side, whether
they match or not. If they match then the columns from both are
returned, if not matched then the columns from one are returned. Not
that MySQL does not support a FULL OUTER JOIN (although there are
ways to emulate it).
A CROSS JOIN joins every combination of 2 tables. These are used when
there is no common column to match on but you want all combinations.
For example if you wanted a table of all employees and all days of
the week for each employee you would cross join a table of days of
the week against a table of employees (then for useful data you might
LEFT OUTER JOIN a table of holidays to the result).
This is the query that I am using to match up a members name to an id.
SELECT eve_member_list.`characterID` ,eve_member_list.`name`
FROM `eve_mining_op_members`
INNER JOIN eve_member_list ON eve_mining_op_members.characterID = eve_member_list.characterID
WHERE op_id = '20110821105414-741653460';
My issue is that I have two different member lists, one lists are members that belong to our group and the second list is a list of members that do not belong to our group.
How do i write this query so that if a member is not found in the eve_member_list table it will look in the eve_nonmember_member_list table to match the eve_mining_op_members.characterID to the charName
I apologize in advance if the question is hard to read as I am not quite sure how to properly ask what it is that I am looking for.
Change your INNER JOIN to a LEFT JOIN and join with both the tables. Use IFNULL to select the name if it appears in the first table, but if it is NULL (because no match was found) then it will use the value found from the second table.
SELECT
characterID,
IFNULL(eve_member_list.name, eve_nonmember_member_list.charName) AS name
FROM eve_mining_op_members
LEFT JOIN eve_member_list USING (characterID)
LEFT JOIN eve_nonmember_member_list USING (characterID)
WHERE op_id = '20110821105414-741653460';
If you have control of the database design you should also consider if it is possible to redesign your database so that both members and non-members are stored in the same table. You could for example use a boolean to specify whether or not they are members. Or you could create a person table and have information that is only relevant to members stored in a separate memberinfo table with an nullable foreign key from the person table to the memberinfo table. This will make queries relating to both members and non-members easier to write and perform better.
You could try a left join on both tables, and then selecting the non-null results from the resulting query -
select * from
(select * from
eve_mining_op_members as x
left join eve_member_list as y1 on x.characterID = y1.characterID
left join eve_member_list2 as y2 on x.characterID = y2.characterID) as t
where t.name is not null
Or, you could try the same thing with a union and using inner join (assuming joined tables are the same):
select * from
(select * from eve_mining_op_members as x
inner join eve_member_list as y1 on x.characterID = y1.characterID
UNION
select * from eve_mining_op_members as x
inner join eve_member_list2 as y2 on x.characterID = y2.characterID) as t
You can throw in your op_id condition where you see fit (sorry, I didn't really understand where it came from). Good luck!
You have several options but by
using a UNION between the eve_member_list and eve_nonmember_member_list table
and JOIN the results of this UNION with your original eve_mining_op_members table
you will get your required results.
SQL Statement
SELECT lst.`characterID`
, lst.`name`
FROM `eve_mining_op_members` AS m
INNER JOIN (
SELECT characterID
, name
FROM eve_member_list
UNION ALL
SELECT characterID
, name
FROM eve_nonmember_member_list
) AS lst ON lst.characterID = m.characterID
WHERE op_id = '20110821105414-741653460';