I apologise for the rushed posting.
The following are images of what I have:
Table 1 called 'players'
Table 2 called 'Reports'
And this is the format that I want the table to be displayed in:
I have tried using simple 'join' and 'and' statments and using some other's that I found on here. Still no avail.
Any help would be great.
Im not sure i understand, but i think that's is what you need:
SELECT `t2`.`id`,`t1`.`name` `Reported Name`, `t12`.`name` `Reporting Name`
FROM `test`.`Reports` `t2`
JOIN `test`.`players` `t1`
on `t1`.`id`=`t2`.`reported_uid`
join `test`.`players` `t12`
on `t12`.`id`=`t2`.`reporting_uid`;
http://sqlfiddle.com/#!2/360d2/1/0
I agree with #Ohah that it's a little unclear, but I'll take a shot at it. If you're just trying to show all of Table 2 with one of the name fields from Table 1, you could do it this way:
SELECT t1.Example_Name, t2.*
FROM
Table_2 t2
JOIN Table_1 t1 ON t1.Name_1 = t2.User_Reported
Table 1 = t1 (t1 is an alias to Table 1 so you don't have to type the whole name out)
Table 2 = t2 (t2 is an alias to Table 2)
"Example_Name" is whichever field you want from the first table
The main question is how the two tables are related and what data you want to get from each. But I hope this helps.
Related
I can't understand whats happening...
I use two sql queries which do not return the same thing...
this one :
SELECT * FROM table1 t1 JOIN table1 t2 on t1.attribute1 = t2.attribute1
I get 10 rows
this other :
SELECT * FROM table1 NATURAL JOIN table1
I get 8 rows
With the NATURAL JOIN 2 rows aren't returned... I look for the missing lines and they are the same values for the attribute1 column ...
It's impossible for me.
If anyone has an answer I could sleep better ^^
Best regards
Max
As was pointed out in the comments, the reason you are getting a different row count is that the natural join is connecting your self join using all columns. All columns are being compared because the same table appears on both sides of the join. To test this hypothesis, just check the column values from both tables, which should all match.
The moral of the story here is to avoid natural joins. Besides not being clear as to the join condition, the logic of the join could easily change should table structure change, e.g. if a new column gets added.
Follow the link below for a small demo which tried to reproduce your current results. In a table of 8 records, the natural join returns 8 records, whereas the inner join on one attribute returns 10 records due to some duplicate matching.
Demo
You need to 'project away' the attribute you don't want used in the join e.g. in a derived table (dt):
SELECT *
FROM table1
NATURAL JOIN ( SELECT attribute1 FROM table1 ) dt;
I have a question about merging a table with another preserving an ID on a database (I'm using MySQL). I have 2 tables, the first has and Item ID and a category and subcategory assigned to that ID. The second has a Item ID with all its characteristics like name and other variables. How can I merge those two tables in a way that the ID corresponds to the correct item in the new table (that's the difficult part I think)? Is it possible?
Thank you for all the help!
It's a very basic operation called Inner Join:
Select *
from table1
inner join table2
on table1.itemid = table2.itemid;
EDIT: As OP wants to create a new table with the fields return by above query and insert data into newly created table; following are the query to insert data once its created:
Insert into tablename
Select *
from table1
natural join table2;
Note: Make sure that the order and datatypes of columns in new table and in the result of above select query must be same.
I'm assuming you want to create table from the combined results. See this page for details.
Basically you write and test the SQL query then CREATE TABLE table_name AS sql_query
create table new_item_table
as
select
a.item_id,
a.category,
a.subcategory,
b.item_name,
b.item_char_1,
b.item_char_2
from
item_category a inner join item_char b on a.item_id = b.item_id;
This will Do:
select a.*,b.ItemName,b.ItemChar1,b.ItemChar2 from FirstTable a join select * from SecondTable b on a.ItemId=B.ItemId;
Use left join if some of the records are not there in the second table
I'm having trouble retrieving latest records from one table based on a query on another table. I've found many solutions which come close using the analogy of "last product ordered for each customer" but they are not perfect.
I have two tables with records linked using the field dailyrecno_i. I would like to retrieve the last entry into table 2 linked to this key.
The tables and desired output are as follows, any advice appreciated.
This is from MS SQL but this should give you an idea. Note that i only replicated your desired output from your sample data so i'm not sure if there is any condition that i missed.
SELECT [#table1].dailyrecno_i ,
MAX([moc_noteno_i]) ,
header ,
MAX([text])
FROM #table1
LEFT JOIN #table2 ON [#table1].dailyrecno_i = [#table2].dailyrecno_i
GROUP BY [#table1].dailyrecno_i ,
header;
I think this is the answer:
SELECT t1.dailyrecno_i, t2temp.moc_noteno_i, t1.header, t2temp.text from table1 t1 inner join (SELECT a.*
FROM table2 a
INNER JOIN (
SELECT dailyrecno_i, MAX(moc_noteno_i) moc_noteno_i
FROM table2
GROUP BY dailyrecno_i
) b ON a.dailyrecno_i = b.dailyrecno_i AND a.moc_noteno_i = b.moc_noteno_i) t2temp
on t1.dailyrecno_i = t2temp.dailyrecno_i group by t2temp.dailyrecno_i;
i'm trying to workout this problem :
I got a table A of default value and a table B of custom value for certain entities. For example :
I got my first table of default value here
And i want to select all of them , but i also want the one from the value table B
I can do that for every entry with a simple join, the problem is that i want only the value of the entity #45, So in the end it would look something like that :
If any ones has any idea on how to do this or at least some references it would be greatly appreciated.
Thanks and have a good day!
Use this
SELECT t1.*
,t2.value
FROM table1 t1
LEFT JOIN
TableB t2
ON t1.id = t2.fk_id_setting
And t2.fk_id_entities=45
Im having a hard time finding anything on Google related to this problem.
What im trying to do is query from multiple tables with an unknown number of values using an IN statement like so...
SELECT * FROM table_1 t1 WHERE t1.t1_id IN ('12345223', '2343374') JOIN table_2 t2 WHERE t2.t2_id IN ('2164158194', '3232422423')
The code above demonstrates what I am trying to achieve. I'm not an SQL guru so im not entirely sure if what i'm going after can be accomplished this way or if there is a much better way to do it. Any help is much appreciated.
Update your query like this:
SELECT *
FROM table_1 t1
JOIN table_2 t2
ON t1.t1_id = t2.reft1_id
WHERE t1.t1_id IN ('12345223', '2343374')
AND t2.t2_id IN ('2164158194', '3232422423')
The "ON" clause will have to contain the two columns that are linked in the two tables.
You got the order of your clauses mixed up. You should go 1.)SELECT, 2.) FROM with JOINs, 3.) WHERE
Like this:
SELECT *
FROM table_1 t1
JOIN table_2 t2
WHERE t1.t1_id IN ('12345223', '2343374')
AND t2.t2_id IN ('2164158194', '3232422423')
But your statement also seems to miss a JOIN-condition so it will either result in an error (it does in oracle) or (assuming t1_id and t2_id are primary keys) give you 4 result lines (seems it does so in mysql):
t1_id t2_id
12345223 2164158194
12345223 3232422423
2343374 2164158194
2343374 3232422423
A JOIN without condition is almost never what you really want and if so it should be explicit in the statement and use CROSS JOIN.