Getting value of Mapping table IDs when value is from same column - mysql

Im really struggling to get my head around what should be simple,
I have two tables, one contains records the other is a mapping table.
records
ID Title Description
1 record 1 desc 1
2 record 2 desc 2
3 record 3 desc 3
4 record 4 desc 4
mapping table
ID1 ID2
1 3
2 4
What I want to do is get the two titles of each row in the mapping table. So the above would output
record 1 record 3
record 2 record 4
Im missing something really obvious, trying multiple joins results in errors trying to link the same table twice.
The following returns NUll
SELECT records.title FROM mapping
LEFT JOIN records
ON mapping.ID1 = records.id
AND mapping.ID2 = records.id

try this one: (UNTESTED)
SELECT b.Title as TitleA,
c.Title as TitleB
FROM mapping a
INNER JOIN records b
on a.ID1 = b.ID
INNER JOIN records c
on a.ID2 = c.ID

Related

MYSQL Inner Join as Multiple Rows Result

I have two tables on my Database: Menu and Dishes. ( I've translated the columns and database name, don't bother if doesn't make sense )
Menu Table
id date_begins date_ends id_dishes_monday id_dishes_tuesday id_wednesday
1 xxxxx xxxxx 1 2 3
Dishes Table
id date dynamic_dishes
1 2016/03/02 BLOB
2 2016/03/03 BLOB
3 2016/03/04 BLOB
I wanted to SELECT the id_dishes_monday, id_dishes_tuesday, id_wednesday From Menu Table and retrieve multiple rows from Dishes Table.
I'm made an attempt using this QUERY but it return only one row and I don't know why.
SELECT D.* FROM Menu M INNER JOIN Dishes D WHERE D.id IN (M.id_dishes_monday,M.id_dishes_tuesday,id_dishes_wednesday) ORDER BY M.id DESC LIMIT 1
What this Query Produces in this Example:
id date dynamic_dishes
1 2016/03/02 BLOB
What I want:
id date dynamic_dishes
1 2016/03/02 BLOB
2 2016/03/03 BLOB
3 2016/03/04 BLOB
Note that the tables datas are just for exemplification, I'd like to know why this Query didn't worked as well the correct Query for it.
Because you are limiting your output!
SELECT D.*
FROM Menu M
INNER JOIN Dishes D
WHERE D.id IN (M.id_dishes_monday,M.id_dishes_tuesday,id_dishes_wednesday)
ORDER BY M.id DESC
I've deleted the LIMIT 1 at the end of your query, which limit the result to the first row

MySQL JOIN performance issue

I am trying to build a query for later inserting it in a table I'm building. I want the query to return 4 columns, which are 4 ids from different entities which relate with each other. I've got one table with the relations, but with varchars, and four tables where I already put the different occurences with an ID each:
TABLE RELATIONS:
A B C D
Sony Bravia 32" 1200€
JVC Whatever 15cm 200€
Samsung Galaxy 13" 500€
TABLE A:
id name
1 Sony
2 JVC
3 Samsung
TABLE B:
id name
1 Whatever
2 Galaxy
3 Bravia
TABLE C:
id name
1 13"
2 15cm
3 32"
TABLE D:
id name
1 200€
2 1200€
3 500€
Now, what I want to get with my query is:
QUERY RESULT:
A B C D
1 3 3 2
2 1 2 1
3 2 1 3
Firstly, I constructed this one:
SELECT DISTINCT A.id as A, B.id as B, C.id as C, D.id as D
FROM relations
INNER JOIN A ON A.name = relations.A
INNER JOIN B ON B.name = relations.B
INNER JOIN C ON C.name = relations.C
INNER JOIN D ON D.name = relations.D
It seems correct, but it takes sooooo long (maybe hours) to complete. The table sizes are (80,65000,1900,15) for the 4 entities, and 65000 for the relations table.
If I perform just one of the joins it takes 15ms, if I perform two of them 6-7 seconds, and with 3 o 4 the time increases exponentially. I think maybe the JOIN solution might be overkill for my situation, as I only need to "translate" the strings...
I already created an index for every "name" field in the four entity tables, as well as an index for relations.a,.b,.c,.d.
Curiously, it takes almost nothing if what I do is duplicate the a,b,c,d columns in the relations table and perform 4 UPDATE queries inserting the id into the duplicate field, matching its "parent"...but I'm sure there's gotta be a better way to do that...anyone has an idea?
Many thanks!
EXPLAIN result http://www.redlanemedia.com/explain.png

Mysql Table Structure Working Fast?

I am planning to create a website similar to IMDB.com. To reduce execution time I am using the following structure. Is it okay for faster working?
Table - 1
Id Movie_name description
1 name one some description
2 name two some description
3 name three some description
Table 2
id actorname
1 name 1
2 name 2
3 name 3
4 name 4
Table 3
id movieid actorid
1 1 1
2 1 2
3 1 3
4 1 9
5 2 6
6 2 5
7 2 8
8 2 1
When I want to list actors in a movie program will retrieve actors ids from table 3 and find respective names from table 2 (using single query). When I want to list the movies of a actor it will retrieve movie ids from table 3 and find respective names from first table. Will it work properly? Any other ideas?
This will give all actors in a specified movie,
SELECT c.ID, c.actorName
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE a.ID = 1
This one will give all movies for a specified actor
SELECT a.*
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID = 1
SQLFiddle Demo (both queries)
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
UPDATE 1
This is called Relational Division
SELECT a.ID, a.Movie_Name
FROM table1 a
INNER JOIN table3 b
ON a.ID = b.movieID
INNER JOIN table2 c
ON b.actorid = c.ID
WHERE c.ID IN (1, 2, 3)
GROUP BY a.ID, a.Movie_Name
HAVING COUNT(DISTINCT c.ID) = 3
SQL of Relational Division
I suggest that you modify table3 by taking away the id field. Use the movieid and actorid together as your primary key. You might want to add other fields to this table such as name of character and order of appearance as suggested in the comment by Jermaine Xu.

How to select records that match values in one column and don't in other column in MySql?

I'm trying to select MerchantIDs that are the same but have different Networks values, for example:
ID MerchantID Network
1 1 A
2 1 A
3 2 B
4 2 C
5 3 D
6 3 D
In that case I would like the query to return "2" (since it's the only MerchantID that have different Networks).
Until now I have the following query:
SELECT a.MerchantID
FROM table a
JOIN table b
ON a.ID = b.ID
AND a.Network <> b.Network
AND a.MerchantID = b.MerchantID
GROUP BY a.MerchantID
Thing is table have around ~43,000 records and that query takes a LOT of time (haven't been even able to get the results).
Is there any better way to do it?
Thanks.
Try this:
SELECT MerchantID
FROM yourtable
GROUP BY MerchantID
HAVING COUNT(Distinct Network)>1
this should be faster, joins that use <> conditions are (usually) slower.

Mysql joining two tables

I need to join the following two tables
table_A
id userId name score game
1 2343 me 45 Palo Alto
2 6575 other 21 SF
3 6575 other 2 miami
table_B
id userId pen mango
1 2343 3 4
2 2343 5 7
3 6575 1 2
Here is the join:
SELECT COUNT(a.userId), SUM(b.pen), SUM(b.mango)
FROM table_A AS a
LEFT JOIN table_B b ON a.userId = b.userId
WHERE userId = 2343;
The problem is I am getting count(userId) equals to 2, but I need it to be 1. What am I doing wrong?
Change it to the following:
count(distinct a.userId)
Your query will gerneate two rows, each row corresponding to one row in table_B (one for id 1, one for id 2).
I am not sure why you think the resulting count(a.userId) should be 1, but you could enforce this by using a GROUP BY clause à la GROUP BY b.userId.
I am confused about what you are doing. You can still get SUM of Pen and Mango without joining the two tables. And another thing, why do you still use the COUNT function where, in fact, you know that you are querying to ONLY ONE ID? Right?
SELECT SUM(Pen) as TotalPen,
SUM(Mango) as TotalMango
FROM table_B
WHERE userId = 2343
But if you want a joined tables you could write something like this:
SELECT SUM(COALESCE(b.pen,0)) as TotalPen,
SUM(COALESCE(b.mango,0)) as TotalMango
FROM table_A AS a LEFT JOIN table_B b ON a.userId = b.userId
WHERE a.userId = 2343;
The problem is I am getting count(userId) equals to 2, but I need it to be 1. - The query is correct but your understanding is wrong. Obviously there are two record IDs of 2343 in Table_B