How to obtain search query using join command in mysql - php

I have two table
Table 1:
Ensemble_ID
Target
Gene_length
miRNA
miRNA_length
mfe
pvalue
position
prediction
no_of_seeds
And in table2
Ensemble_ID
local_ID
I want to display the result in the following order,
Ensemble_ID, local_ID, Ensemble_ID, Target ,Gene_length, miRNA, miRNA_length, mfe, pvalue, position, prediction, no_of_seeds
But i could not join it .. Can some one help ??

This is what you are asking:
SELECT t1.Ensemble_ID AS Ensemble_ID1, t2.local_ID, t2.Ensemble_ID AS Ensemble_ID2,
t1.Target, t1.Gene_length, t1.miRNA,
t1.miRNA_length, t1.mfe, t1.pvalue, t1.position, t1.prediction, t1.no_of_seeds
FROM table1 t1, table2 t2
But it think you want something like this:
SELECT t2.local_ID, t2.Ensemble_ID, t1.Target, t1.Gene_length, t1.miRNA,
t1.miRNA_length, t1.mfe, t1.pvalue, t1.position, t1.prediction, t1.no_of_seeds
FROM table1 t1
INNER JOIN table2 t2 ON (t2.Ensemble_ID = t1.Ensemble_ID)
If you want to join table1 and table2 on Ensemble_ID there is no need to output it twice

Related

How to Avoid SubQuery with same output in MySql

Good Day!
I already saw other post about avoiding subquery / using JOIN statement, but still I can't figure it out why my query is so very slow to execute the 9 result data. but when the result data is more than thousands the query execute 0.7k ms only.
My question is , how can I speed up this query execution, what will I remove/add, Or do I need to re-construct the query? how?
here's my query
SELECT a.ts, b.bc, b.rem, c.dept FROM table1 a
INNER JOIN (select doc, max(ID) from table1 Group By doc) d ON d.doc=a.doc AND d.ID=a.ID
INNER JOIN table2 c ON c.u_id=a.u_id
INNER JOIN table3 b ON b.id=a.doc
WHERE c.depart = 'deparment' AND b.end = 0
here is the Screen Shot of
EXPLAIN result
Updated SS for EXPLAIN result
I already set INDEX :
ALTER TABLE table3 ADD INDEX max_id (end,id,bc);
ALTER TABLE table1 ADD INDEX max_id (ID,doc,u_id,ts,rem);
ALTER TABLE table1 ADD INDEX m_id (doc,ID);
ALTER TABLE table2 ADD INDEX user_max (dept,u_id);
Try a correlated subquery:
SELECT a.ts, b.bc, b.rem, c.dept
FROM table1 a INNER JOIN
table2 c
ON c.u_id = a.u_id INNER JOIN
table3 b
ON b.id =a.doc
WHERE c.depart = 'deparment' AND b.end = 0 AND
d.id = (SELECT MAX(t1.id) FROM table1 t1 WHERE t1.doc = a.doc);
For the correlated subquery you want an index on table1(doc, id).

PHP SQL Query Fields from Another Table Join

I am trying to get a query using fields from 2 tables.
I need to query Table1 but only Table2 has the variable venue_location that I need to query.
Basically I need to count all records on Table1 where Table1.venue_location = $MyVariable.
Here is what I've put together but I believe I need to use Joins for this?
Table1
- venue_id
Table2
- venue_id,
- venue_location
SELECT * FROM Table1 WHERE table1.venue_id = table2.venue_id and table2.location = '$MyVariable'
How can I do a query for this?
Use the power of join table
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
You can get back the count of rows with mysqli_num_rows() in PHP, or change le select by SELECT COUNT(*) AS nbRow FROM ... and check of value in nbRow column
You can join two tables on venue_id and then group it by venue_id where location is your $MyVariable.
Your final query will look like:
SELECT count(table2.venue_id)
FROM Table1
JOIN Table2 ON table1.venue_id = table2.venue_id
WHERE table2.location = '$MyVariable'
GROUP BY table2.venue_id
try this
SELECT Table1.venue_id, Table2.venue_location FROM Table1 INNER JOIN Table2
ON Table1.venue_id='$MyVariable';

To find realted data of table1 in table2

Table1 Has Some data as Categories
Table2 Has Some data Which is Realated to table1 Categories
and the relation between two tables is cat_id from table1 and cat_ids from table2.
What I want is?
I need to display all fields in table1 and from table2 I need only the related content i.e id's present in cat_id(table1) and cat_ids(table2)
I am using a query like this select c.* ,cc.* from news_categories cc, news_content c where cc.cat_id = c.cat_ids group by cc.cat_id this gives only common data from table1 and table2.. i need common data and all categories from table1
can anyone help me?
You should use JOIN instead.
SELECT t1.*, GROUP_CONCAT(t2.content_id)
FROM table1 t1
LEFT JOIN table2 t2
ON t2.cat_ids = t1.cat_id
GROUP BY t1.cat_id
This is for all fields of both tables...
SELECT Table1.*, Table2.*
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
This is for all fields of Table1 and Content field of Table2...
SELECT Table1.*, Table2.full_content
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
you need to use outer join and this case left join .. Have a look at Documentation

Select unmatched records from two tables of MYSQL

I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;

Help with grasping (INNER?) JOIN

I'm having trouble building a query. I can do what I want in 3 different queries.
SELECT id FROM table1 WHERE url LIKE '%/$downloadfile'
put that in $url_id
SELECT item_id FROM table2 WHERE rel_id = '$url_id'"
put that in $item_id
SELECT rel_id FROM table2 WHERE rel_id = '$item_id' AND field_id = '42'"
put that in $user_id
But from reading examples on joins and inner joins I think there's a more elegant way. I cant wrap my brain around writing a better query (but would like to) I can describe how it should go:
table1
fields: id, url
table2
fields item_id, rel_id, field_id
I know the last part of table1.url (LIKE '%/$filename') with that I select table1.id.
table1.id is equal to one entry in table2.rel_id. So get that and select the table2.item_id.
In table2 there is another entry which has the same table2.item_id and it will have a table2.field_id = '42'
And finally the value I need is the table2.rel_id where the table2.field_id was 42.
I will fetch that value and put it in $user_id
Can this be done with one query using joins/inner joins?
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.rel_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'
Sure, should be something like:
SELECT t2_second.rel_id
FROM table1 t1
INNER JOIN table2 t2_first ON t1.id = t2_first.rel_id
INNER JOIN table2 t2_second ON t2_second.rel_id = t1_first.item_it
WHERE
t1.url = '%/:filename' AND
t2_second.field_id = 42
You could even throw in a distinct so that each t2_second.rel_id is only returned once:
SELECT DISTINCT [...]
You might not need this, however, if t2_second.field_id = 42 will restrict the query to only return one row for each t2_second.rel_id
This the query that works for me, made one small change to Ignacio's answer:
SELECT url, second.rel_id AS user_id
FROM table1
INNER JOIN table2 AS first
ON table1.id=first.rel_id
INNER JOIN table2 AS second
ON first.item_id=second.item_id
WHERE second.field_id='42'
AND table1.url LIKE '%/:downloadfile'

Categories