this seems like a very easy question and in fact I know how to help myself but I hope to find a quicker approach to this problem.
I have two tables with the same structure in an sql-dbms, whereas some lines of data occur in both tables. My way of finding these duplicates is making an inner-join with all columns in the "on-statement", like:
create table target as
select
a.column_1
a.column_2
a.column_3
...
a.column_20
from table source_1 as a
inner join source_2 as b
on a.column_1 = b.column_1 and
a.column_2 = b.column_2 and
...
a.column_20 = b.column_20
;
With a large amount of columns in the tables, this produces a long piece of code. Is there some kind of syntax or method to make this shorter?
Thanks for your help,
kind regards.
If your tables are of same structure then try using the following query,
select * from source_1
INTERSECT
select * from source_2
it will give you the common rows from both the tables
Related
I just can't see the problem with how I'm making my foreign keys and I'm just really confused why I keep getting the wrong result. Here are screenshots from my workbench
Here are my tables:
And here's my diagram
I've also tried to normalize my tables and I was kinda expecting my query to return a similar result like in the sample table (Questions table) where it will only show 2 results since I want to query where idsurvey = 1 I made in this image:
My question is that, how do I fix my foreign key so that if I want to query
select * from survey.survey, survey.questions where idsurvey = 1
it will only return 2 rows? (based on sample data in the workbench screenshot)
Any comments and suggestions on my diagram would also be greatly appreciated.
When you have two tables in the from clause, every row from the first table is matched with every table from the second table. This is known as a Cartesian Product. Usually, this isn't the behavior you'd want (like it isn't in this case), and you'd use a condition to tell the database how to match these two tables:
SELECT *
FROM survey.survey s, survey.questions q
WHERE s.idsurvey = q.survey_id AND idsurvey = 1
While this should work, it's quite outdated to use multiple tables in the same from clause. You should probably use an explicit join clause instead:
SELECT *
FROM survey.survey s
JOIN survey.questions q ON s.idsurvey = q.survey_id
WHERE idsurvey = 1
I have two tables, I've been trying to print the result from each but they are being duplicated. These are the two MySQL tables and the result. Notice the duplication.
The sql code for the project is:
SELECT * FROM savings,savtype WHERE cust_id=".$_SESSION['user']
I'm also looking for a work around this, in the meantime, id appreciate any assistance on this.
because you are not specifying how the two tables are related. You need to add that, either via an explicit ... JOIN ... (USING|ON)
SELECT
*
FROM
savings JOIN savtype USING (savtype_id)
WHERE
cust_id = ".$_SESSION['user']
or by providing the criteria in the where clause.
SELECT
*
FROM
savings, savtype
WHERE
savings.savtype_id = savtype.savtype_id AND
cust_id = ".$_SESSION['user']
As I understand from the screenshot you added, it makes joint between those tables, and what you probably want it left join from savings and savtype tables.
SELECT *
FROM `savings`
LEFT JOIN `savtype`
ON savings.savtype_id=savtype.savtype_id
where cust_id=".$_SESSION['user'] .";
Update if this did the trick,
You can learn more about left join here: https://www.w3schools.com/sql/sql_join_left.asp
I have a view called "xml_links" which is in the following format
model_number fk_link sd_link
10R46 www.fk.com/10R46 www.sd.com/10R46
10R47 www.fk.com/10R47 www.sd.com/10R47
And a table called "prohub"
page_url fk_price sd_price
www.fk.com/10R46 $155
www.sd.com/10R46 $161
www.fk.com/10R47 $117
www.sd.com/10R47 $146
I'm trying to join them using the following query and all I get is a blank table.
select
xml_links.model_number,
prohub.fk_price,
prohub.sd_price
from
xml_links, prohub
where
xml_links.fk_link=prohub.page_url
and
xml_links.sd_link=prohub.page_url
I'm looking for the following result:
model_number fk_price sd_price
10R46 $155 $161
10R47 $117 $146
Thanks for your help
They query that you are looking for
SELECT model_number,
(SELECT flipkart_price FROM pro_hub WHERE page_url=flipkart_link and flipkart_price is not null),
(SELECT snapdeal_price FROM pro_hub WHERE page_url=snapdeal_link and snapdeal_price is not null)
FROM xml_links
But this is really a patchwork solution for a database that neesd to be normalized
The solution that you are really looking for
Is a redesign of your tables.
In a proper RDBMS one does not have repetitive data. In your database values like 'www.fk.com/10R46' repeat not in just one table (which is bad enough but in two tables).
Secondly you are storing values in columns rather than in rows. The solution I have given you will work very well as long as you have only two vendors but what happens when you add a third vendor? YOu need to add a third column to both of your tables. That might takes hours if you have millions of records and during that time the site will be unresponsive.
what about this, is this what you needed?
select
model_number
, sum(fk_price) as fk_price
, sum(sd_price) as sd_price
from xml_links xml
INNER JOIN prohub pro
on right(pro.page_url, 5) = xml.model_number
GROUP BY model_number;
I have these two tables:
Achievement:
Achieves:
Question:
I want to retrieve rows from table Achievement. But, I do not want all the rows, I want the rows that a specific Steam ID has acquired. Let's take STEAM_0:0:46481449 for example, I want to check first the list of IDs that STEAM_0:0:46481449 has acquired (4th column in Achieves table states whether achievement is acquired or not) and then read only those achievements.
I hope that made sense, if not let me know so I can explain a little better.
I know how to do this with two MySQL statements, but can this be done with a single MySQL statement? That would be awesome if so please tell me :D
EDIT: I will add the two queries below
SELECT * FROM Achieves WHERE Achieves.SteamID = 'STEAM_0:0:46481449' AND Achieves.Acquired = 1;
Then after that I do the following query
SELECT * FROM Achievement;
And then through PHP I would check the IDs that I should take and output those. That's why I wanted to get the same result in 1 query since it's more readable and easier.
In sql left join, applying conditions on second table will filter the result when join conditions doesn't matter:
Select * from achievement
left join achieves on (achievement.id=achieves.id)
where achieves.acquired=1 and achieves.SteamID = 'STEAM_0:0:46481449'
Besides,I suggest not using ID in the achieves table as the shared key between two tables. Name it something else.
I don't think a left join makes sense here. There is no case where you don't want to see the Achievement table.
Something like this
SELECT *
FROM Achieves A
JOIN Achievement B on A.ID = B.ID
WHERE A.SteamID = 'STEAM_0:0:46481449'
AND A.Acquired = 1;
I had an issue joining multiple tables to retrieve the data I needed. In order to accomplish the proper results I had to first create a view (shown below) called: vwinvgrossrev :
SELECT dbo.inv_item.inv_num, dbo.inv_item.co_line,
dbo.inv_hdr.co_num, dbo.inv_hdr.inv_date, dbo.inv_item.qty_invoiced,
dbo.inv_item.price
FROM dbo.inv_item INNER JOIN
dbo.inv_hdr ON dbo.inv_item.inv_num = dbo.inv_hdr.inv_num
and then I had to join the view on my final table in order to create a proper summation of the values that I wanted
select sum(vwinvgrossrev.qty_invoiced*vwinvgrossrev.price)
from vwinvgrossrev,coitem
WHERE vwinvgrossrev.co_num=coitem.co_num
AND coitem.Uf_Erne='Y'
AND vwinvgrossrev.co_line=coitem.co_line
AND DATEPART(mm,vwinvgrossrev.inv_date) = DATEPART(mm,Getdate())
AND YEAR(vwinvgrossrev.inv_date) = YEAR(Getdate())
My question is this. Is there anyway to do this in a single query. The problem is all 3 tables have a many to many relationship with one another and always returns the wrong value when joining all 3 tables.
If your current query (using the view) gives you what you are looking for then I believe that this query will give you the same thing in a single query:
SELECT sum(inv_item.qty_invoiced*inv_item.price)
FROM inv_item
JOIN inv_hdr ON dbo.inv_item.inv_num = dbo.inv_hdr.inv_num
JOIN coitem ON (inv_hdr.co_num=coitem.co_num
AND inv_item.co_line=coitem.co_line)
WHERE coitem.Uf_Erne='Y'
AND vwinvgrossrev.co_line=coitem.co_line
AND DATEPART(mm,inv_hdr.inv_date) = DATEPART(mm,Getdate())
AND YEAR(inv_hdr.inv_date) = YEAR(Getdate())
However, I'll be surprised if that is what you are looking for.
I am assuming that you don't actually have a many-to-many between all 3. I am assuming that inv_hdr has a unique inv_num and inv_item has many of those. Similarly I am assuming that co_num is unique within inv_hdr but has multiple occurrences within coitem. These are not many-to-many but rather 1-to-many relationships. Since SELECT multiplies rows I think you are going to be summing way more than you want.
Why do you want to include coitem at all if you are just interested in inv_item.qty_invoiced and inv_item.price? Is it just to limit on the Uf_Erne column? Is coitem always going to have either 1 or 0 records for that co_num and co_line with Uf_Erne='Y'?
In sum, I've reproduced your query in a single query without a view, but I think you may need to think through whether this is really what you want.
You need to refine your database schema to normalise out the many-to-many relationships. That is a given and is standard practice regardless of your current problem. The fact that it will simplify your current problem is the reason why. There are many tutorials/blog/etc out there to show you how to do this with join tables so I am not going into it here.