JOIN or Union of two tables - SQL - mysql

How can I unite two select statement in one table result?
For instance in the first table I want to get everything however on my 2nd table I only want the corel name that is equal to the corel_id and id of my 2nd table?
SELECT *
FROM garage
UNION
SELECT c.name
FROM corel as c
WHERE EXISTS (SELECT 1 FROM garage as g WHERE c.id = g.corel_id
I tried to execute this but this did not work. Is this right? or is there a better way to do this?
Sorry newbie here.
UPDATE EXPECTED RESULT :
https://anotepad.com/notes/b6662w

Give this a try:
SELECT g.*, c.name
FROM garage g
LEFT JOIN corel c
ON c.id = g.corel_id
Matching two tables in a database is called a join. An inner join, the default, returns only the rows that match from both tables.
A left join returns all the rows from the first table whether or not they match the second, and any data from the second table that matches. The right join does the inverse, returning only non-matching data from the second table. There is also the full join that returns all data regardless of match.

A join statement is what you need. A join puts columns from multiple tables into rows together based in the matching conditions in the where clause.
A union requires 2 or more queries to have the same columns. The union puts the sets of rows together into a longer set or rows.

Related

select records from 3 tables with same column id

I am trying to write an SQL query which will select records of a student within 3 tables that have the same column_I'd.
This is what I wrote but the the records selected are not accurate:
select
Nov_DEC_billing.*,
Nov_DEC_students_portfolio.*,
admission_form.academic_year
from
Nov_DEC_billing,
Nov_DEC_student_portfolio,
admission_form
where
Nov_DEC_billing.ID = Nov_DEC_student_portfolio.ID=admission_form.ID
AND
admission_form.Program ='Nov/dec'
I get a records selected alright but its not accurate. Please what's the right way to join 3 tables that share the same column_id.???
Use JOIN in your query
SELECT b.*, p.*, a.academic_year
FROM Nov_DEC_billing b
JOIN Nov_DEC_student_portfolio p ON p.id = b.id
JOIN admission_form a ON a.id = b.id
WHERE a.Program='Nov/dec'
You need to join tables something like this:
SELECT Nov_DEC_billing.*,
Nov_DEC_students_portfolio.*,
admission_form.academic_year
FROM Nov_DEC_billing AS ndb,
LEFT JOIN Nov_DEC_student_portfolio AS ndsp ON ndsp.ID=ndb.ID,
LEFT JOIN admission_form AS af ON af.ID=ndb.ID
WHERE af.Program='Nov/dec'
You should join all the tables to a single one.
What you will is join all the tables to a single one and then select from it.
Since you have 2 tables you should first join 2 and then join another one on the result.
See here left join example for the exact syntax.
Nov_DEC_billing.ID=Nov_DEC_student_portfolio.ID=admission_form.ID
doesn't do what you expect. It takes the first part, Nov_DEC_billing.ID=Nov_DEC_student_portfolio.ID and evaluates it. If the values match, that part becomes a 1, if they don't match, it becomes 0. Then the 0 or the 1 is compared against admission_form.ID. That is very likely to give strange results.
So you'd have to split that into:
Nov_DEC_billing.ID=Nov_DEC_student_portfolio.ID
AND Nov_DEC_student_portfolio.ID=admission_form.ID
Or just use explicit join syntax, as the others already advised (and which I do too). That forces you to split this anyway.

MySQL does not select all data from one table that is Left Join with another table?

In one of my PHP application, I use left join between two MySQL tables using the following query:
SELECT bs.question_code, bs.question_english, count( st.id ) sqid
FROM bs_qbank_question bs
LEFT JOIN bs_qbank_question_study_link st
ON bs.id = st.question_id
The problem is, it selects only those rows from bs_qbank_question that have at least one row in bs_qbank_question_study_link table. If there is no data for a particular row of bs_qbank_question in bs_qbank_question_study_link then that row is not selected.
But I need to select each row from bs_qbank_question and also need one column for each row from bs_qbank_question_study_link as count the total occurrence of that row.
Can anyone tell me what's wrong with my query and how can I replace count( st.id ) as 0 if there is no data for any row?
- Thanks

Retrieving common column data from two tables in a single column with out duplicates in MYSQL

Below are the 2 tables from which i want to retrieve common column ie "location" from two tables in to a single column with out duplicates... I am using this query..
SELECT datan1.location,
temp.location
FROM datan1
LEFT OUTER JOIN TEMP ON datan1.location=temp.location
UNION
SELECT datan1.location,
temp.location
FROM datan1
RIGHT OUTER JOIN TEMP ON datan1.location=temp.location;
but i am not getting what i expected... any help is highly appreciated
Expected Output
Location
1.Mysore
2.Hyderabad 3. Chennai
Hyderabad should not come as it is repeated
if you want common locations between the two tables you should use inner join
select location
from datan1
inner join temp on temp.location = datan1.location
if you want all the location in both the table you could use a simply union avoinding left and right join
select location
from datan1
union
select location
from temp

How to select from multiple tables when some of the tables are empty in MySQL

I need to fetch data from 5 tables(all columns of each table) all have FK, which is PK of single table.
But some of the tables may have record may be empty.If data is present on the respective column/table it should return otherwise null/default value
There is one to many and one to one relations on the child tables with parent table.
I have tried so far
- UNION which has concern of same number of columns
- CROSS JOIN not returning any data
- SELECT ALL_COLUMN FROM ALL_TABLE WHERE TABLE.FK=ID Not returning any data
- LEFT JOIN working for 2 tables but not more than that
SELECT A.GENDER, B.BLOCKED_USER FROM t_macroworld_registration AS A
LEFT JOIN t_macroworld_blacklist AS B ON 1=1 WHERE A.ID=15
What are the possible ways I can implement this in a view in MySQL.
Outer join operations are the normative pattern...
SELECT ...
FROM a
LEFT JOIN b ON b.a_id = a.id
LEFT JOIN c ON c.a_id = a.id
LEFT JOIN d ON d.a_id = a.id
WHERE a.id = 15
It's important for the predicates on the outer joined tables to be in the ON clause and not the WHERE clause. If there's any predicate in the WHERE clause requires that a value from one of the outer joined tables be non-NULL, that will negate the "outerness" of the join, making it into an inner join.
The "big rock" problem with this the result when there are more than one matching rows in b, c and d. If there's five rows from b that match, and three rows from c that match, and two rows from b that match, it's going to look like a lot of duplicates. (5x3x2 = 30 rows to be returned, with a lot of duplicated data on those rows.)
Finally I have solved It,
I broke the whole thing into many select query based on FK from each table, so number of additional row returns and mapping has become easy.
Who ever is getting this kind of problem, if it is possible then break it into many select query instead of one.
SELECT
w.id,w.name,a.address,i.name,i.quantity
FROM
warehouse w
LEFT JOIN address AS a ON a.warehouse_id = w.id
LEFT JOIN item AS i ON i.warehouse_id = w.id
LEFT JOIN order AS o ON o.item_id = i.id
WHERE
w.id = 1
GROUP BY 1,2,3,4;
Will give you an overview of your stock and orders for your warehouses. This will also duplicate some results.
Assuming 2 warehouses, 1 address for each, 3 items per warehouse, 2 orders by item = 2 * 3 * 2 = 12 lines
I recommend adding your LEFT JOIN stage by stage and visualizing the result for each stage. You'll quickly understand why lines are multiplying.
Note the usage of foreign keys and ids in the tables to link the tables.
Good luck

mysql join 2 tables, second table needs SUM()

I have 2 tables,
first contains articles, second contains quantities. the two tables are linked with the "kodex" column. The second table can have multiple records for the same item, but can also have none.
what I need is a query that lists the entire first table, and adds an additional column that contains the sum of all quantities of all entries in the second table.
I have done this with left join on the kodex table, and works fine, but only as long as i do not add the sum() on the select statement. as soon as I do so, it lists only the rows that have a match on the second table.
Query that displays all rows:
SELECT b.* FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex
query that displays only rows with matching entries in the second table:
SELECT b.*, sum(e.menge) as gesmenge FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex
what I would need is the behaviour of the first query, with the additional column gesmenge from the second query.
thanks!
Update your query
SELECT b.*, sum(e.menge) as gesmenge FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex
with adding group by b.article_id. Group by is needed so query know how to summarize all quantities of all entries in the second table.
Final query should look like
SELECT b.*, sum(e.menge) as gesmenge FROM `bestehend` as b left join eingelesen as e on e.kodex=b.kodex group by b.article_id