I am trying to join 2 tables and in 1 table there may or may not have the corresponding values.but i need to join the table and list the fields as null.
I have tried to join the tables as left join.but If two entries is there in secoond table corresponding to the value of first table,then first table data is displayed twice.
I need to display only one time if there is two data in another table or there is no data in another table,but it should display as null.
Here is my sql query.
SELECT *,incident.incident_id as incidentid
FROM register_incident AS incident
LEFT JOIN incident_images AS im ON im.incident_id=
incident.incident_id
WHERE incident.state='Active'
I need to display only one time each data if there is no corresponding rows in another table,but the fields in the second table list as null.
If there is more than one row in another table,then also display each row in first table one time with one entry in the second table.
you can use select distinct for get only one row eg (limiting the select to indicent_id ,, but you can add the distinctcolumn you need ):
SELECT *,incident.incident_id as incidentid
FROM register_incident AS incident
LEFT JOIN (
select distinct incident_id from
incident_images
WHERE incident.state='Active' ) t ON t.incident_id= incident.incident_id
Related
I have a problem with my query. I've managed to display data from 2 tables. Next I want to add up SALE_AMT from the 3rd table, the problem is that order by cannot, the program output adds up all data not by order by
This my query:
SELECT
customer.CUST_NO, customer.CUST_NM,
store.STR_CD, store.STR_NM, SUM(SALE_AMT)
FROM
customer, store, cust_sale_pnt
GROUP BY
CUST_NO
table customer
table cust_sale_pnt
table store
My result
my result
Can you guys help me to sum data based on grouping cust_no?
The problem is, that you don't specify a condition that's used to combine the records of the different tables. In effect, you're creating a 'cross join'.
That is, any record of table customer is combined with any record of table store and the result is combined with any record of table cust_sale_pnt.
In effect, you get each of your 3 customers combined with each of your 6 sales. Therefore the sum over sale_amt is identical for each customer.
You want an INNER JOIN to combine the records:
SELECT c.CUST_NO, c.CUST_NM, SUM(csp.SALE_AMT)
FROM customer c
INNER JOIN cust_sale_pnt csp ON c.CUST_NO = csp.CUST_NO
INNER JOIN store ON csp.STR_CD = store.STR_CD
GROUP BY c.CUST_NO, c.CUST_NM;
I have two relations, emp(id,name) and emp2(name,city,salary) with values:
and relation emp2 values :
As you can see, when I run natural join between them I get their Cartesian product.
But why, since they have a common attribute (name)?
You are getting natural join, not Cartesian product.
There is one copy of each common column and one copy of each column unique to an input table. There is a row for every different combination of a row from the left input table and a row from a right input table.
If a subrow value (('kim')) for common columns (name) appears n times in the left table (2 times) and m times in the right table (2 times) then it will appear n times m times in the output (2 times 2 times = 4 times).
An SQL Cartesian product would have columns name, city, salary, id & name and would have a row for every different combination of a row from the left input table and a row from a right input table.
That includes every possible combination of a name value from the left table and the right table. But for this example data there is only one name value over both tables, so every pair of name values has to have that value twice.
Natural join is the same as Cartesian product followed by restriction on equality of common columns followed by projecting away one of each common column followed by keeping distinct rows.
select * from emp inner join emp2 on emp.id = emp2.id
You have two time the same value in the common column (name) so this retrive the rows more time .
Your natural join is based on both the table that have not unique value each row.
Then in this case you get always more that a row for each entry (also with inner join) but you could solve using distinct
select dictinct emp.*, emp2.*
from emp
inner join emp2 on emp.name = emp2.name
or
select select dictinct emp.*, emp2.*
from emp2 natural join emp
i have three tables
meeting (id,name,weekly_plan_id,is_agent)
weekly_plan (id,plan)
meeting_agent (meeting_id,agent_id) this is mapping table
meeting_contact (meeting_id,contact_id) this is mapping table
now what i want? to fetch table based on if condition in meeting tables is_agent field if it is 1 then i want to fetch data from
meeting_agent table else from meeting_contact
SELECT m.*,IF(m.is_agent='1',ma.agent_id, mc.contact_id) AS pid
FROM
meeting m
JOIN weekly_plan wp ON wp.id=m.weekly_plan_id
JOIN meeting_agent ma ON ma.meeting_id=m.id
JOIN meeting_contact mc ON mc.meeting_id=m.id
WHERE wp.id=9
Then you have to specify this logic over and over for each column :
SELECT m.*,
IF(m.is_agent='1',ma.agent_id, mc.contact_id) AS pid,
IF(m.is_agent='1',ma.col1, mc.col1) ,
IF(m.is_agent='1',ma.col2, mc.col2) ,
..........
Or use LEFT JOIN with this condition inside the ON clause, and one of the tables columns will be null and the other will be filled with data.
I have two tables, called "One" and "Two", they are both the same, table "One" has some IDs that are not there in Table "Two". but when i run this mysql Query, i can not get those non-matching values.
select count(bene) from one where bene not in (select bene from two);
And the result is Zero. so how do i get the non-matching values from Table one that are not there in table Two?
You can perform a left join and find only values that do not exist in second table. Below query is assuming that bene is your ID column:
SELECT one.*
FROM one
LEFT JOIN two ON one.bene = two.bene
WHERE two.bene IS NULL
or if you need just the count
SELECT COUNT(one.bene)
FROM one
LEFT JOIN two ON one.bene = two.bene
WHERE two.bene IS NULL
When performing a Left join you find all values in table one with corresponding values in table two. If there is no match all table two values will be NULL.
Then as a second step you use WHERE to filter only rows that have NULL for table two. This way you find all records in table one that do not have matching records in table two.
select count(*) from one a where not exists (select * from two b where a.bene=b.bene)
query:
select DOG_CD,ANIMAL_CD
from FKAGM
where FKAG = 12024
Displays 3 rows
DOG_CD - ANIMAL_CD are column names and below each column is 3 numerical values yielded from the above query & I have no clue how to draw a table on here to depict that. =(
There is a table called Dog_Animal that has a column called "Name" (Dog_Animal.Name) that I want to join with this above query. I want to join on ANIMAL_CD as FKAGM table and the DOG_ANIMAL table have the ANIMAL_CD column in common. I'd like to display "Name" right next to the ANIMAL_CD column. The issue is when I join the tables it displays like every instance of DOG_CD and ANIMAL_CD within the Dog_Animal table (Which is thousands) something similar to the below illustration. since the Dog_Animal table has thousands of DOG_CD and ANIMAL_CODE fields populated. I just want it to be limited to the three rows that are returned by the above query (Limit or distinct or something) and input the Dog_Animal.Name next to Animal_CD. I've been working on this for an hour and I know it must be so simple, but I can't seem to get it to work. I am not sure if a subquery is needed or exists, or case, or what. If you can figure this out I would be so thankful!
DOG_CD - ANIMAL_CD - Dog_Animal.Name with the same 3 rows of data just now a name included
select
f.DOG_CD,f.ANIMAL_CD,d.name
from FKAGM f
inner join dog_animal d
on d.animal_cd = f.animal_cd
where f.FKAG = 12024;
inner join would work for you. If you only need to limit it to 3 results, add limit 3 order by f.dog_cd at the end of your query.