I have two tables in my database. "A" table is the main table that stores user primary information and other table "B" stores if user wants to add some additional address onto their profile.
Structure of both tables(has common columns with exact same names) is as per below picture except some diff columns that are not shown in this pic
Now i want to display Address stored in Table A as well as in Table B
I used below queries but all these return only values from Table B
Query 1 :
Select t1.(star),t2.(star) from `b` t2 , `a` t1 WHERE t1.emailbc = ?;
Query 2 :
Select t1.(star),t2.(star) from `a` t1
INNER JOIN `b` t2 ON (a.emailbc=b.emailbc)
WHERE t1.emailbc = ?
I also tried NATURAL Join but that does not work either. Please let me know solution.
If you want to display all addresses in one column, but coming from both tables, you need to use a UNION. Try this:
SELECT *
FROM table1
WHERE emailbc = ?
UNION
SELECT *
FROM table2
WHERE emailbc = ?
Change your join query to the following.
Select t1.*,t2.* from `a` t1
INNER JOIN `b` t2 ON (t1.emailbc=t2.emailbc)
WHERE t1.emailbc = ?
Related
I have 3 different tables in db those are like tbl1, tbl2, tbl3
in these all table have one same columns 'direction'.
tbl1 contain one value either tbl2 or tbl3,
tbl2 contains one value tbl3.
How can I select the rows from tbl1 and tbl2 which contains direction like equal tbl3.
i also include my two table image in this image 2 columns name is same level_redir now i want which row that have value like hardening
enter image description hereenter image description here
please suggest me which query is good for getting row from two different tables in which table columns where dir_redirect = 'hardening'.
Since your question is not clear and you have not included all the table structure its bit difficult to figure out.
Let me help you with generic way
The following query we have joined all the 3 tables based on the common tables with are present in the respective tables. The following query select all the columns from all the 3 tables.
SELECT * FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';
You can select from specific tables by the following
SELECT t1.col1, t1.col2, t2.col1, t2.col2 FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';
I'm doing some maintenance work on a database application and I've discovered that, joy of joys, even though values from one table are being used in the style of foreign keys, there's no foreign key constraints on the tables.
I'm trying to add FK constraints on these columns, but I'm finding that, because there's already a whole load of bad data in the tables from previous errors which have been naively corrected, I need to find the rows which don't match up to the other table and then delete them.
I've found some examples of this kind of query on the web, but they all seem to provide examples rather than explanations, and I don't understand why they work.
Can someone explain to me how to construct a query which returns all the rows with no matches in another table, and what it's doing, so that I can make these queries myself, rather than coming running to SO for every table in this mess that has no FK constraints?
Here's a simple query:
SELECT t1.ID
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.ID IS NULL
The key points are:
LEFT JOIN is used; this will return ALL rows from Table1, regardless of whether or not there is a matching row in Table2.
The WHERE t2.ID IS NULL clause; this will restrict the results returned to only those rows where the ID returned from Table2 is null - in other words there is NO record in Table2 for that particular ID from Table1. Table2.ID will be returned as NULL for all records from Table1 where the ID is not matched in Table2.
I would use EXISTS expression since it is more powerful, you can e.g. more precisely choose rows you would like to join. In the case of LEFT JOIN, you have to take everything that's in the joined table. Its efficiency is probably the same as in the case of LEFT JOIN with null constraint.
SELECT t1.ID
FROM Table1 t1
WHERE NOT EXISTS (SELECT t2.ID FROM Table2 t2 WHERE t1.ID = t2.ID)
SELECT id FROM table1 WHERE foreign_key_id_column NOT IN (SELECT id FROM table2)
Table 1 has a column that you want to add the foreign key constraint to, but the values in the foreign_key_id_column don't all match up with an id in table 2.
The initial select lists the ids from table1. These will be the rows we want to delete.
The NOT IN clause in the where statement limits the query to only rows where the value in the foreign_key_id_column is not in the list of table 2 ids.
The SELECT statement in parenthesis will get a list of all the ids that are in table 2.
Let we have the following 2 tables(salary and employee)
Now i want those records from employee table which are not in salary.
We can do this in 3 ways:
Using inner Join
select * from employee
where id not in(select e.id from employee e inner join salary s on e.id=s.id)
Using Left outer join
select * from employee e
left outer join salary s on e.id=s.id where s.id is null
Using Full Join
select * from employee e
full outer join salary s on e.id=s.id where e.id not in(select id from salary)
Where T2 is the table to which you're adding the constraint:
SELECT *
FROM T2
WHERE constrained_field NOT
IN (
SELECT DISTINCT t.constrained_field
FROM T2
INNER JOIN T1 t
USING ( constrained_field )
)
And delete the results.
From similar question here MySQL Inner Join Query To Get Records Not Present in Other Table I got this to work
SELECT * FROM bigtable
LEFT JOIN smalltable ON bigtable.id = smalltable.id
WHERE smalltable.id IS NULL
smalltable is where you have missing records, bigtable is where you have all the records. The query list all the records that not exist in smalltable but exists on the bigtable. You could replace id by any other matching criteria.
I Dont Knew Which one Is Optimized (compared to #AdaTheDev
) but This one seems to be quicker when I use (atleast for me)
SELECT id FROM table_1 EXCEPT SELECT DISTINCT (table1_id) table1_id FROM table_2
If You want to get any other specific attribute you can use:
SELECT COUNT(*) FROM table_1 where id in (SELECT id FROM table_1 EXCEPT SELECT DISTINCT (table1_id) table1_id FROM table_2);
You could opt for Views as shown below:
CREATE VIEW AuthorizedUserProjectView AS select t1.username as username, t1.email as useremail, p.id as projectid,
(select m.role from userproject m where m.projectid = p.id and m.userid = t1.id) as role
FROM authorizeduser as t1, project as p
and then work on the view for selecting or updating:
select * from AuthorizedUserProjectView where projectid = 49
which yields the result as shown in the picture below i.e. for non-matching column null has been filled in.
[Result of select on the view][1]
You can do something like this
SELECT IFNULL(`price`.`fPrice`,100) as fPrice,product.ProductId,ProductName
FROM `products` left join `price` ON
price.ProductId=product.ProductId AND (GeoFancingId=1 OR GeoFancingId
IS NULL) WHERE Status="Active" AND Delete="No"
SELECT * FROM First_table
MINUS
SELECT * FROM another
How to select rows with no matching entry in Both table?
select * from [dbo].[EmppDetails] e
right join [Employee].[Gender] d on e.Gid=d.Gid
where e.Gid is Null
union
select * from [dbo].[EmppDetails] e
left join [Employee].[Gender] d on e.Gid=d.Gid
where d.Gid is Null
I'm stuck in a piece of code that does not quite understand.
I have several tables with different names but same fields, but the tables are independent
Something like that:
table1
id
user
title
table2
id
user
title
I need to get in the same query data from two tables but I fail, I try with INNER JOIN, UNION ALL, but not knowing, it misapplied.
Right now I have this:
$mysites = $db->QueryFetchArrayAll("
select *
FROM table1,table2
where table1.user = table2.user AND
table1.user = 1");
foreach($mysites as $mysite){
echo $QUERY['title'];
}
but returned this:
title1.table1
title2.table1
and i like this:
title1.table1
title2.table1
title1.table2
title2.table2
A greeting and thanks
You can use the keyword UNION like this:
SELECT * FROM table1 UNION SELECT * FROM table2
This query will select everything from table1 and merge the results with those from table2. Please note that you have to select the same number of columns from both tables. Moreover, column names and datatypes will be assigned according to first table.
If you want to preserve duplicates add the keyword ALL:
SELECT * FROM table1 UNION ALL SELECT * FROM table2
The question is very unclear.....
Are the ID's the same in each table for each user? If so an INNERJOIN will help
SELECT t1.*, t2.*
FROM table1.t1
INNER JOIN table2.t2
ON t1.id = t2.id
WHERE t1.user = "1"
(Change INNER JOIN to LEFT JOIN if the data could be missing)
If this is not the case, why not put the data from one table into the other, and have just one table with all the data in it?
What I'm trying to do should be really simple. I have two tables with the following columns:
Table 1:
Name, Level
Table 2:
Name, Cost
Name is the primary key. I want to combine both table's data into one table that has all three columns. What I've been trying to do is add a Cost column into Table 1 and copy all the Cost values from Table 2 into it. I've tried numerous suggestions from other threads on this site and I've never had one work for me. The new Cost column in Table 1 never budged with any new values. Why?
I am doing this on MySQL Workbench on Ubuntu.
Here's one that I tried using (New cost column already made for Tbl1):
UPDATE Tbl1
SET Cost = (
SELECT Cost
FROM Tbl2
WHERE Name = 'SpecificName')
WHERE Name = 'SpecificName;
This works when I specify individual rows but it doesn't work when I replace Name = 'SpecificName' with something like "Tbl1.Name = Tbl2.Name"
The problem you face is when some names are in one table but not the other. Here is one method, using a single create table as:
create table NameLevelCost as
select n.name, t1.level, t2.cost
from (select name from table1
union
select name from table2
) n left join
table1 t1
on t1.name = n.name left join
table2 t2
on t2.name = n.name;
This assumes that name is unique in each of the tables.
update Tbl1 t1 left join Tbl2 t2 on t2.Name = t1.Name set t1.Cost = t2.Cost;
i have spent the last 2 days going over this problem trying to work it out.
i have two tables
one is the user_login and it has a row called user_id the other is agedcare and it has a row called id
i have tried to get the 2 to link together
i want to be able to update the agedcare using the id from user_login as the main id
$query = "SELECT id FROM agedcare INNER JOIN user_id ON login_users = login_users.user_id WHERE login_users.user_id = '$id'"
The format is something more like..
SELECT id,name,age
FROM Table1
INNER JOIN Table2
ON Table1.Field=Table2.Field
Another way to simplify the select statement (so that you avoid using the table names throughout) is to "rename" the table right after you name it. For example, the line that has "FROM Table1 T1" - the T1 immediately after the table name is "renaming" the table allowing you to shorten the table name when you need to refer to it. In addition, you can use the t1 and t2 identifiers when selecting the fields.
SELECT t1.id,t1.name,t2.age
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.Field=T2.Field
Check your format of join, it must be like this
SELECT * FROM t1 as a LEFT JOIN t2 as b ON a.id=b.id where user_id = 'id'