I have the next tables:
date Income AccIncome
--------------------------------
2016-10-1 10 10
2017-11-1 20 30
date Qty AccQty
--------------------------------
2016-10-1 2 2
2017-11-1 4 6
date Ava AccAva
--------------------------------
2016-10-1 3 3
2017-11-1 4 7
I need to obtain:
date Income AccIncome Qty AccQty Ava AccAva
------------------------------------------------------
2016-10-1 10 10 2 2 3 3
2017-11-1 20 30 4 6 4 7
I could use a select using all these tables but how could this be done with a JOIN? Could the JOIN be much faster than using just a SELECT over all these tables picking up just the fields I need?
You can use inner join on date if the joining values in rows always match
select a.date, a.income, a.AccIncome, b.Qty, b.AccQty, c.Ava, c.AccAva
from table1 a
inner join table2 b on a.date= b.date
inner join table3 c on a.date = c.date
or left join if can not match
select a.date, a.income, a.AccIncome, b.Qty, b.AccQty, c.Ava, c.AccAva
from table1 a
left join table2 b on a.date= b.date
left join table3 c on a.date = c.date
if you use only a select withou join and on condtion you obtain a cartesia product of all the rows .. so in your case instead of two row as result .. you get 8 rows
and the inner/left join is normally much more faster that a cross join ( a select over all table) because work on reduced set o rows.. for help the join performance is useful a proper indexinig of the rows
the on clause in join and the same condition in where clause do the same work .. is only a diffrent sintax in the first case you have an explict join sintax more clear to read in the secondo you have an inplicit join .
Related
Result Right Now , want to ignore dulicate
id_category name id_manufacturer name
6 CLOTH of House 12 Sabb
6 CLOTH of House 12 Sabb
6 CLOTH of House 14 CTES
8 Sabih Nopoe 12 Sabb
I want this Result remove duplicate and heading changes
id_category Man-name id_manufacturer Cat-name
6 CLOTH of House 12 Sabb
6 CLOTH of House 14 CTES
8 Sabih Nopoe 12 Sabb
SELECT
p.id_category_default,
c.name,
p.id_manufacturer,
d.name
FROM
psup_product p
INNER JOIN psup_manufacturer d ON
p.id_manufacturer = d.id_manufacturer
INNER JOIN psup_category_lang c ON
p.id_category_default = c.id_category
WHERE p.id_category_default > 2
GROUP BY p.id_product
ORDER BY p.id_category_default
Your code show you're working on PrestaShop table
this code will work please check
SELECT
p.id_category_default,
c.name as Man-name,
p.id_manufacturer,
d.name as Cat-name
FROM
psup_product p
INNER JOIN psup_manufacturer d ON
p.id_manufacturer = d.id_manufacturer
INNER JOIN psup_category_lang c ON
p.id_category_default = c.id_category
WHERE p.id_category_default > 2
GROUP BY d.id_manufacturer
ORDER BY c.id_category
You can remove duplicates by selecting DISTINCT values or by GROUPing. DISTINCT will return only distinct values across all the columns you selected. GROUPing will group rows into summary rows by the fields you group by.
Here either adding DISTINCT after your SELECT statement, or including all the fields in your SELECT statement in your GROUP BY statement should return distinct records. Because you grouped by id_product which is not in the SELECT statement, it will not remove duplicates.
Examples in code would be:
Updating SELECT statement to include DISTINCT
SELECT DISTINCT --Adding DISTINCT keyword
p.id_category_default,
or, updating GROUP BY statement to include all fields in the SELECT statement.
GROUP BY --Removed p.id_product from the grouping and added fields from SELECT statement.
p.id_category_default,
c.name,
p.id_manufacturer,
d.name
I've got two mySQL tables, Table A and B. I need to get an output like in Table 3.
Below mentioned is the code I tried with Full Join and does not give me the intended result. Much appreciate your help..
SELECT DISTINCT(Table_A.Code) as 'Code', SUM(Table_A.Qty_On_Hand) as 'On Hand Qty', SUM(Table_B.Counted_Qty) as 'Counted Qty'
FULL JOIN Table_B ON Table_A.Code = Table_B.Code
FROM Table_A
Table A
Code
On Hand Qty
A
20
B
10
B
20
B
50
C
60
Table B
Code
Counted Qty
A
10
B
0
C
30
B
0
C
10
Out put required:
Code
On Hand Qty
Counted Qty
A
20
10
B
80
0
C
60
40
You need to use GROUP BY Table_A.Code, not DISTINCT.
SELECT a.Code, SUM(a.Qty_On_Hand) AS `On Hand Qty`, b.`Counted Qty`
FROM Table_A as a
JOIN (
SELECT Code, SUM(Counted_Qty) AS `Counted Qty`
FROM Table_B
GROUP BY Code
) AS b ON a.Code = b.Code
GROUP BY a.Code
You need to do one of the SUMs in a subquery, otherwise you'll multiply its sums by the number of rows in the other table. See Join tables with SUM issue in MYSQL.
I have two tables:
data
id[int] balance[float] category[id]
1 10.2 1
2 0.12 2
3 112.42 1
4 2.3 3
categories
id[int] name[varchar] start_at[float]
1 high 10.5
2 low 105.2
3 mid 0.7
I want to query the categories and join the data. For each categorie I want the sum of all data balances added to the start_at value of categories:
This is where I started with:
select sum(d.balance) as balancesum, c.name
from data d
left join categories c on c.id = d.category
group by d.category
What I want to know is, how can I add the start_at value of categories to the balancesum value?
SELECT c.name, c.start_at + SUM(d.balance) as balancesum
FROM categories c
JOIN data d ON c.id = d.category
GROUP BY c.name, c.start_at
You can use next approach:
select
c.name, balancesum, ifnull(balancesum, 0) + start_at
from categories c
left join (
-- calculate sum of balances per category
-- and join sums to data table
select category, sum(d.balance) as balancesum
from data d
group by d.category
) b on b.category = c.id;
Here you can play with live query
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.
If I have 2 tables:
A B
joe 1
joe 2
kevin 3
B C
1 1
1 2
1 3
2 2
2 3
3 3
What is the best way to get the subgroups when I search for column A?
i.e. for joe, i want to return 1:{1,2,3} and 2:{2,3}.
I know that I can iterate through multiple SELECT * FROM queries, but is there a way to do it in one query?
As a followup,
If I had a third table,
C D
1 x
2 y
3 z
How do I table 2 and table 3 together and then group by B?
I tried
select
tbla.id, tbla.name, group_concat(tblb.value)
from tbla
left join tblb
on tbla.id = tblb.a_id
group by tbla.id
left join tb1c
on tb1b.id=tb1c.id
and it does not seem to work
group_concat
E.g.
select
tbla.id, tbla.name, group_concat(tblb.value)
from tbla
left join tblb
on tbla.id = tblb.a_id
group by tbla.id ;
you might want to try this.
SELECT GROUP_CONCAT(TableB.C) as iResult
FROM tableB INNER JOIN tableA
on TableB.B = tableA.B
WHERE tableA.A = 'joe'