I'm new to mysql and I'm learning join queries now. when I compare strings I got weird output that mentioned below. I have two tables
MariaDB [test]> select * from classroom;
+---------+-----------+
| subject | classroom |
+---------+-----------+
| maths | 1 |
| englishs| 2 |
+---------+-----------+
Table student:
MariaDB [test]> select * from student;
+------+------+---------+
| id | name | subject |
+------+------+---------+
| 1 | abc | maths |
| 2 | abcd | english |
+------+------+---------+
I have tried this query
select b.classroom,a.name,b.subject from student a left join classroom b
on a.subject = b.subject ;
and the output is like,
+-----------+------+---------+
| classroom | name | subject |
+-----------+------+---------+
| 1 | abc | maths |
| NULL | abcd | NULL |
+-----------+------+---------+
I don't understand why am getting second row if the strings are doesn't match in both tables.
This has nothing to do with string comparison.
You are using an outer join, but the result you are expecting is the one that inner joins gives.
Take a look at this post for a good explanation about inner and outer joins.
From that post:
An inner join of A and B gives the result of A intersect B, i.e. the inner part of a Venn diagram intersection.
An outer join of A and B gives the results of A union B, i.e. the outer parts of a Venn diagram union.
try this may be this will work.
select b.classroom,a.name,b.subject from student a,classroom b where a.subject = b.subject
Related
I'm trying to join two tables together in MySQL. I need to join based on the full string from one field (Table a:ID) and the sub-string from the other table (Table b:caseID). A mock-up of the table structure can be seen at the bottom of this post. ID and caseID are defined as unique.
The output I'm looking for is similar to:
|-------------|--------------|------------|
| ID | name | age |
|-------------|--------------|------------|
| 1 | Bob | 22 |
| 2 | Bill | 23 |
| 3 | Ben | 24 |
|-------------|--------------|------------|
I know how to extract a substring based on a delimiter:
SELECT SUBSTRING(caseID, LOCATE('-', caseID)+1, LENGTH(caseID)) AS ExtractString FROM b
but I'm unclear how to combine this with the usual SQL JOIN statement to return all joined records. I keep getting error like 'returns more than one row'.
Any help much appreciated.
Table a:
|-------------|--------------|
| ID | name |
|-------------|--------------|
| 1 | Bob |
| 2 | Bill |
| 3 | Ben |
|-------------|--------------|
Table b:
|-------------|--------------|
| caseID | age |
|-------------|--------------|
| 24-1 | 22 |
| 24-2 | 23 |
| 24-3 | 24 |
|-------------|--------------|
SELECT *
FROM a
LEFT JOIN b ON a.ID = SUBSTRING(b.caseID, LOCATE('-', b.caseID)+1, LENGTH(b.caseID))
SELECT *
FROM a
JOIN b ON a.ID = SUBSTRING_INDEX(b.caseID, '-', -1)
Typical, as soon as I ask the question I work out the answer!
SELECT a.*, b.*
FROM a
JOIN b
ON b.id = SUBSTRING(a.caseID, LOCATE('-', a.caseID)+1, LENGTH(a.caseID))
This question already has answers here:
Understanding the number of matched rows in LEFT JOIN
(5 answers)
Closed 4 years ago.
Here is my table structure:
// mytable
+----+---------+----------+
| id | related | subject |
+----+---------+----------+
| 1 | NULL | subject1 |
| 2 | 1 | |
+----+---------+----------+
And there are two queries which seem identical to me, but have different results in tests:
SELECT a.id, IFNULL(b.subject, a.subject)
FROM mytable a
LEFT JOIN mytable b ON a.id = b.related
+----+----------+
| 1 | subject1 |
| 2 | |
+----+----------+
SELECT a.id, IFNULL(b.subject, a.subject)
FROM mytable a
LEFT JOIN mytable b ON b.id = a.related
+----+----------+
| 1 | subject1 |
| 2 | subject1 |
+----+----------+
Look, it is self-join. So why the result of ON a.id = b.related and ON b.id = a.related is different?
Running your queries with SELECT * to uncover some of the mystery:
Your first query:
SELECT *
FROM mytable a
LEFT JOIN mytable b ON a.id = b.related;
Produces the following:
+----+---------+----------+--------+----------+----------+
| id | related | subject | id1 | related1 | subject1 |
+----+---------+----------+--------+----------+----------+
| 2 | 1 | <null> | <null> | <null> | <null> |
| 1 | <null> | subject1 | 2 | 1 | <null> |
+----+---------+----------+--------+----------+----------+
Your second query:
SELECT *
FROM mytable a
LEFT JOIN mytable b ON b.id = a.related;
Produces this:
+----+---------+----------+--------+----------+----------+
| id | related | subject | id1 | related1 | subject1 |
+----+---------+----------+--------+----------+----------+
| 2 | 1 | <null> | 1 | <null> | subject1 |
| 1 | <null> | subject1 | <null> | <null> | <null> |
+----+---------+----------+--------+----------+----------+
Your first query is joining id 2 to related 2. There is no related 2 and since id 2 has no subject, you get no subject out of your ifnull().
Your second query is joining related 1 to id 1 for a.id 2. This pulls a subject from b.id 1 and you get a subject back for id 2 as a result.
You really have to mentally map out how a LEFT JOIN works here and how it is affected by your ON clause. You have two very different queries here as a result.
Both queries are getting all rows from a.
Both queries are doing an outer join to b.
What's different is the condition that is used for finding a "match" from b.
(The queries might seem to be identical, but the truth is that they are significantly different.)
As a demonstration, run a query like this:
SELECT a.id AS `a_id`
, a.related AS `a_related`
, a.subject AS `a_subject`
, b.id AS `b_id`
, b.related AS `b_related`
, b.subject AS `b_subject`
FROM mytable a
LEFT
JOIN mytable b
ON b.related = a.id
And then change the ON clause
ON b.id = a.related
You might also want to repeat both of those queries removing the LEFT keyword (to make it an inner join instead of an outer join.)
One way to look at an outer join... when a matching row from b is not found, a dummy row from b is invented. That dummy row consists entirely of NULL values, and the dummy row is joined to a, as if it were a matching row. (This isn't necessarily what the database engine actually does, but thinking about it this way gives us an insight to the results that the outer join returns.)
Take a close look at the results of the queries, and you will be able to see why the results by the queries are different.
The fact that a and b refer to the same table is a special case. We would see the same results if those were two different tables. It really doesn't matter... to the query, those are two different sources which just happen to refer to the same table. Don't let the fact that a and b refer to the same table cause any confusion.
I have three tables
Table a
+-----+-------+
| aid | value |
+-----+-------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
+-----+-------+
Table b
+-----+------+
| bid | name |
+-----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+-----+------+
Table ba (mapping of table a and table b)
+-----+-----+
| bid | aid |
+-----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 3 | 2 |
| 1 | 3 |
| 2 | 3 |
| 2 | 4 |
+-----+-----+
From these tables I want a query like
SELECT aid, mapped('true'-if(aid exist in ba) 'false'-otherwise)
FROM a
JOIN b
JOIN ba
WHERE bid=1
to get a result from where I can generate a list
(when bid=1)
A-mapped
B-not mapped
C-mapped
D-not mapped
(when bid=2)
A-mapped
B-not mapped
C-mapped
D-mapped
(when bid=3)
A-mapped
B-mapped
C-not mapped
D-not mapped
Right now I am generating the list in a while loop for all the rows of table 'a' and inside the loop a query is executed for each iteration to check the existence in table 'ba'.
I think this is supposed to be table b independent:
SELECT CONCAT_WS('-', a.value, IF(ba.aid IS NULL, "-not mapped", "-mapped"))
FROM a LEFT JOIN ba ON a.aid = ba.aid AND ba.bid = 1
ORDER BY a.aid
Note: I took "a" table as the base table since your samples included all values from "a" table.
This is a tricky question, but the difficult part is in figuring out how to formulate the query. Once that is out of the way, it is downhill from there. One approach is to use a cross join between the A and B tables to obtain all possible mappings. Then LEFT JOIN to the mapping table to determine which pairs are being mapped and which are not. Try the following query:
SELECT tb.bid, ta.value,
CASE WHEN ba.bid IS NOT NULL THEN 'mapped' ELSE 'not mapped' END AS label
FROM tb INNER JOIN ta -- cross join to obtain all bid/aid pairs
LEFT JOIN ba -- to determine which pairs are mapped/not mapped
ON ta.aid = ba.aid AND tb.bid = ba.bid
ORDER BY tb.bid, ta.value
Demo here:
SQLFiddle
I'm a little bit confused about which join to apply and where.
I have a mysql database that does betting in an IRC client.
It stores usernames , their guess and the eventual outcome of the game they bet on
the outcomes_table is like this
+--------------+
| id outcome |
+--------------+
| 1 win |
| 2 lose |
+--------------+
the user_table is like this
+----+----------+----------+-------------------+
| id | username | guess_id | bettingsession_id |
+----+----------+----------+-------------------+
| 1 | name1 | 1 | 1 |
| 2 | name2 | 1 | 2 |
| 3 | name3 | 2 | 2 |
4 name1 1 2
+----+----------+----------+-------------------+
the betting_session_table is like this:
+----+---------+
| id | result |
+----+---------+
| 1 | 1 |
| 2 | 2 |
+----+---------+
I want to get a list of the bets of a user with their guess joined to the outcome and the result joined to the
eg:
select each row a different bet username, guess (win/lose), result (win/lose)
Something like:
SELECT *
FROM user_table
INNER JOIN betting_session_table ON bettingsession_id = betting_session_table.id
INNER JOIN outcomes_table ON guess_id = outcomes_table.id
INNER JOIN outcomes_table ON result = outcomes_table.id
WHERE username = 'name1'
However this doesn't work, not sure but I don't think it lets me join the outcomes_table.id twice to two different columns but I want to this because the user may bet 'win' but result 'lose' etc.
EG: I want to return
+----+----------+----------+----+---------+--------------------+----+--------+----+---------+
| id | username | guess_id | id | outcome | betting_session_id | id | result | id | outcome |
+----+----------+----------+----+---------+--------------------+----+--------+----+---------+
| 1 | name1 | 1 | 1 | win | 1 | 1 | 1 | 1 | win |
| 4 | name1 | 1 | 1 | win | 2 | 2 | 2 | 2 | lose |
+----+----------+----------+----+---------+--------------------+----+--------+----+---------+
EDIT:
In the end I used two separate alias for each join which seems to work; here is the code from the actual table that works rather than the cut down example above.
SELECT *
FROM `xcoins_betting_log` A
LEFT JOIN `xcoins_betting_session` B ON A.betting_session_id = B.id
LEFT JOIN `xcoins_common_tables`.`xcoins_betting_outcomes` C ON A.guess_id = C.id
LEFT JOIN `xcoins_common_tables`.`xcoins_betting_outcomes` D ON B.outcome_id = D.id
WHERE `user_id` =9
I'm not sure if this is what you want, but I hope so.
SELECT
usr.*,
res.outcome,
IF(res.id = usr.guess_id, 'User win', 'User lose') AS result
FROM user_table AS usr
INNER JOIN betting_session_table AS bet ON
bet.id = usr.bettingsession_id
INNER JOIN outcomes_table AS res ON
res.id = bet.result
WHERE usr.username = 'name1'
Choose correct join
The most common joins is LEFT and INNER. Lets say the users have placed their bets, but the football game (or whatever) isn't completed yet, then you won't have the row in the outcomes_table right? The game isn't finished so the results will come later.
If you use INNER JOIN, the row in the outcomes_table won't match for unfinished games --- INNER JOIN requires matches.
If you want to see the bets also before the game has started, you can use LEFT JOIN. LEFT JOIN won't remove rows that hasn't got any outcome, the users will still be listed --- LEFT JOIN doesn't care.
INNER JOIN: Game must have result
LEFT JOIN: Game might have result
I have the query:
SELECT `gigs`.*, COUNT(`signups`.`signupID`) AS `signupsPending` FROM `gigs` NATURAL JOIN `signups` WHERE (`signupStatus` = 4) GROUP BY `gigID`
That is querying a database that looks like this:
+-------+---------+------------+
| gigID | gigName | gigDate |
+-------+---------+------------+
| 1 | Foo | 01/01/2014 |
+-------+---------+------------+
| 2 | Bar | 16/01/2014 |
+-------+---------+------------+
+----------+-------+--------------+--------------+
| signupID | gigID | signedUserID | signupStatus |
+----------+-------+--------------+--------------+
| 1 | 1 | 1 | 1 |
+----------+-------+--------------+--------------+
| 2 | 1 | 2 | 4 |
+----------+-------+--------------+--------------+
| 3 | 1 | 3 | 2 |
+----------+-------+--------------+--------------+
| 4 | 2 | 1 | 2 |
+----------+-------+--------------+--------------+
But when I do the query above, it only shows a row for gigID = 1. How can I alter the above query so it will show 0 with the rest of the row?
Use LEFT OUTER JOIN instead of NATURAL JOIN.
SELECT `gigs`.*, COUNT(`signups`.`signupID`) AS `signupsPending`
FROM `gigs`
LEFT OUTER JOIN `signups`
WHERE (`signupStatus` = 4) GROUP BY `gigID`
I have been trying OUTER JOIN's, but that has not yielded any results, but I have now tried:
SELECT `gigs`.*, COUNT(`signups`.`signupID`) AS `signupsPending` FROM `gigs` LEFT JOIN `signups` ON `gigs`.`gigID` = `signups`.`gigID` AND `signupStatus` = 4 GROUP BY `gigID`
And that is working great
You want to use LEFT JOIN (to which you can add OUTER for ANSI-92 compatibility)
SELECT
gig.*
, COUNT(sig.signupID) AS "signupsPending"
FROM gigs AS gig
LEFT JOIN signups AS sig ON (gig.gigID = sig.gigID)
WHERE sig.signupStatus = 4
GROUP BY gig.gigID
Btw I advise you not to use NATURAL JOIN because it brings several risks
It's based on field names and you may have different field names as foreign keys
Different foreign keys can be used to JOIN 2 tables (even if you can add a USING statement)
During your database life tables can get field changed added, and you will always have to check that both tables don't get new fields with the same name
If you use both NATURAL and standard JOINs it can be difficult to automate your queries through scripts
...