MySQL - faster NOT EXISTS/EXISTS values from the same table - mysql

I have 2 tables: tbl1 and tbl2. I want to return a single row from tbl1 with columns: col1, col2, col3, can_be_deleted, have_important_items. The idea is that can_be_deleted and have_important_items columns are boolean values resulted (both) by searching in the same table tbl2.
SELECT
col1,
col2,
col3,
NOT EXISTS(SELECT 1 FROM tbl2 WHERE mycategory=10 AND status>0 LIMIT 1) AS can_be_deleted,
EXISTS(SELECT 1 FROM tbl2 WHERE mycategory=10 AND type_item>0 AND status>0 LIMIT 1) AS have_important_items
FROM tbl1 WHERE ... LIMIT 1
To avoid later clarifications, tbl2 columns are:
mycategory - a value to group items inside table
status - enabled/disabled item
type_item - 0-not important, >=1 important one (scale of importance)
Question: Can I write a faster query?

using JOIN is faster than EXIST,check if this query solve your probleme and if not then just change the conditions to get your result.
"t1.t2_id" is the foreign key of tbl2, change it to the correct name
SELECT
col1,
col2,
col3,
LEFT JOIN tbl2 AS t2 ON t2.id = t1.t2_id
WHERE t2.mycategory = 10
AND t2.status>0
AND t2.type_item>0
FROM tbl1 AS t1 WHERE ... LIMIT 1

Related

mysql storing subquery in column and performing where condition

i am performing a sub query in mysql which is like
select col1, col2 , (select col3 from table2) as 'data'
from table1
where not data is null
how should i get data in where clause. IS it POSSIBLE
One way to do this is :
SELECT *
FROM (
select col1, col2 , (select col3 from table2) as 'data'
from table1
)t
WHERE data IS NOT NULL
As you see there I have created on derived table t for your query, now result of your query is treated as Table(temp table) and having columns as col1,col2 and col3, Using this result set we can able to access col3 in where clause .
Note - assuming that select col3 from table2 returns single value as per OP's comments
Use cross join:
select t1.col1, t1.col2, t2.col3 as data
from table1 t1 cross join
(select col3 from table2) t2
where t2.col3 is not null;

Join 2 tables with all same column headers but one different

How do I join 2 tables that have all of the same column headers but one?
The other 3 columns are all the same then one table has the column header "January" and the other table has the column header "February"
So I would like to end up with 5 total columns in new table
I would also like all of the data in there even if there are duplicates. Before I used a union all but now I cannot do that because not all my headers are the same.
Thanks!
You can use UNION and rename one of your columns:
SELECT id, name, age, xxx FROM table1
UNION
SELECT id, name, age, yyy AS xxx FROM table2
If you add more information, I can elaborate my answer
select tb1.col1, tb1.col2, tb1.col3, tb2.col3 from tb1
inner join tb2 on (tb1.col1 = tb2.col1)
Assuming uniqueness when just considering the three columns, joining them
SELECT tab1.col1,
tab1.col2,
tab1.col3,
tab1.january,
tab2.february
FROM tab1,
tab2
WHERE tab1.col1 = tab2.col1
AND tab1.col2 = tab2.col2
AND tab1.col3 = tab2.col3
This assumes all rows in tblA have corresponding ones in tblB and vice versa:
SELECT * FROM tblA INNER JOIN tblB USING (col1, col2, col3);
If that is not the case, and depending on the data (if tbl1.january can have legitimate null values, this won't work), you could try something like (didn't check for sure, might need massaged with parenthesis, subselecting, etc...):
SELECT *
FROM tbl1
LEFT JOIN tbl2 USING (col1, col2, col3)
UNION ALL
SELECT * FROM tbl2 LEFT JOIN tbl1 USING (col1, col2, col3)
HAVING tbl1.`january` IS NULL
;

Merging two SQL Server tables conditionally into a third table

Clearly, I am not a SQL guy, so I have to ask for help on the following rather simple task.
I have two SQL Server 2008 tables: t1 and t2 with many identical columns and a key column (entry_ID). T2 has rows that do not exist in t1 but should.
I want to merge those rows from t2 that do not exist in t1 but I also do not want any rows from t2 that already exist in t1. I would like the result set to fill a new t3.
I have looked at many solutions online but can't find the solution to the above scenario.
Thank you.
There are a number of ways to do it you could use UNION ALL or OUTER JOIN.
Assuming you are using Entry_ID to find identical records, and Entry_ID is unique within each table, here is a OUTER JOIN method:
This gets you your recordset: T1 and T2 merged:
SELECT
CASE
WHEN T1.Entry_ID IS NULL THEN 'T2'
WHEN T2.Entry_ID IS NULL THEN 'T1'
ELSE 'Both'
END SourceTable,
COALESCE(T1.Entry_ID,T2.Entry_ID) As Entry_ID,
COALESCE(T1.Col1, T2.Col1) As Col1,
COALESCE(T1.Col2, T2.Col2) As Col2,
COALESCE(T1.Col3, T2.Col3) As Col3,
COALESCE(T1.Col4, T2.Col4) As Col4
FROM T1 FULL OUTER JOIN T2
ON T1.Entry_DI = T2.Entry_ID
ORDER BY COALESCE(T1.Entry_DI,T2.Entry_ID)
This inserts it into T3:
INSERT INTO T3 (Entry_ID,Col1, COl2,Col3,Col4)
SELECT
COALESCE(T1.Entry_DI,T2.Entry_ID) As Entry_ID,
COALESCE(T1.Col1, T2.Col1) As Col1,
COALESCE(T1.Col2, T2.Col2) As Col2,
COALESCE(T1.Col3, T2.Col3) As Col3,
COALESCE(T1.Col4, T2.Col4) As Col4
FROM T1 FULL OUTER JOIN T2
ON T1.Entry_DI = T2.Entry_ID
Again you must note that Entry_ID needs to be unique within their tables, and it uses this to match between the tables.
Also note the columns from the select line up with the column list in the insert statement - the order of the columns in the physical table doesn't matter, the INSERT and SELECT just have to line up.

Add values from Table1 into Table2 without adding duplicates

[MySQL 5.5]
I have two tables - Table_1 and Table_2.
They have identical columns - Col1, Col2, Col3, Col4.
Table_1 can have duplicates on columns Col1 and Col2.
Example-1:
Col1,Col2,Col3,Col4
1) a ,b ,c ,1
2) a ,b ,d ,2
Now Table_2 has the following rows:
Example-2:
Col1,Col2,Col3,Col4
1) a ,b ,e ,1
2) a ,c ,f ,2
I want to write all rows from Table_2 into Table_1 that do not have duplicates on Col1 and Col2. In the above instance, the insert should ignore row 1 in Example-2 above and add row 2 since there are no duplicates for combination (a,c) in Table_1.
Adding Unique keys on Col1 and Col2 will not work as it will delete row no 2 in Example 1.
Both Table_1 and Table_2 have 2 million rows each. Nested select statements(which I tried) have spelled disaster in terms of execution time.
Is there another way out of this?
This should do:
INSERT INTO Table_1
SELECT *
FROM Table_2 A
WHERE NOT EXISTS(SELECT 1 FROM Table_1
WHERE Col1 = A.Col1
AND Col2 = A.Col2)
insert into table1
select *
from table2
where concat(Col1,Col2) not in
(
select concat(col1,col2)
from table1
) as T
See below. Its using join so will have better performance.
INSERT INTO Table_1
SELECT T2.Col1
,T2.Col2
,T2.Col3
,T2.Col4
FROM Table_2 T2
LEFT JOIN Table_1 T1
ON T2.Col1 = T1.Col1
AND T2.Col2 = T1.Col2
WHERE T1.Col1 IS NULL
AND T1.Col2 IS NULL
Find the rows that don't exist via LEFT JOIN and NULL checking in the WHERE clause
INSERT INTO Table_1 (Col1, Col2, Col3, Col4)
SELECT Table_2.Col1, Table_2.Col2, Table_2.Col3, Table_2.Col4
FROM Table_2
LEFT JOIN Table_1
ON Table_2.Col1 = Table_1.Col1
AND Table_2.Col2 = Table_1.Col2
WHERE Table_2.Col1 IS NULL;

Needs help in database query

I have two tables Table1 and Table2. There are 10 fields in Table1 and 9 fields in Table2. There is one common column in both the tables i.e. AdateTime. This column saves unix time stamp of user actions. I want to display records from both the tables as a single result but sorting must me according to AdateTime. Recent action should be display first. Sometimes many recent actions in Table1 but few in Table2. Vice versa is also possible. So I want to fetch combine result set from both the tables using single query. I am using PHP MySQL.
Try
SELECT t1.*, t2.*
FROM table1 t1 INNER JOIN table2 t2
ON t1.AdateTime = t2.AdateTime
ORDER BY t1.AdateTime
or (if tables are not related)
SELECT * FROM
(SELECT ADateTime, col1, col2, col3, col4 FROM table1
UNION
SELECT ADateTime, col1, col2, 1 AS col3, NULL AS col4 FROM table2) t2
ORDER by ADateTime
I would use UNION ALL with an inline view. So something like
select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime
from
(
select col1,col2,col3,col4,col5,col6,col7,col8,col9,AdateTime from Table1
UNION ALL
select col1,col2,col3,col4,col5,col6,col7,col8,null as col9,AdateTime from Table2
) t
order by t.Adatetime desc;
yes you can do it. you just need to join these 2 tables with a join condition. when the join condition matches for a row only that row ll be displayed then further you can write the Code for any operation. use order by AdateTime
select t1.column_1234,t2.column_1234
from t1 table1 , t2 table2
where t1.matching_column = t2.matching_column
order by t1.AdateTime;
t1.matching_column And t2.matching_column are the Primary And Foreign keys for these tables (Matching Column)