I have two tables. The left side table is Bin and the Right side table is Bout. In_id means an order in which they are batting, Out_id means an order in which they are out from the game. Report1 below shows the answer I want, i.e. those who made a partnership. I am not getting any idea on how to write a query to retrieve the data which is in report 1. That is the answer I actually want. How to write a query for that? Your help would be appreciated!
This is multi step approach. table 1 refers to left side table i.e. batting id table and table 2 refers to right side table i.e out id table in your data.
new.table1, new.table2, new.table3, new.table4 are temporary table. Correct column name as per your data. Hope you will get your result. Comment if find any issue.
Create temporary table new.table1
select OUT.ID, IN.ID, Name, (IN.ID - OUT.ID) AS DIFF from table2;
Create temporary table new.table2
select OUT.ID, IN.ID, Name, (OUT.ID + 1) AS NEW.ID from new.table1 where DIFF <= 0;
Create temporary table new.table3
select OUT.ID, IN.ID, Name, DIFF AS NEW.ID from new.table1 where DIFF > 0;
Create temporary table new.table4
select OUT.ID, IN.ID, Name, NEW.ID from new.table2
UNION ALL
select OUT.ID, IN.ID, Name, NEW.ID from new.table3;
---Final Output
select A.Name, B.Name AS Name2 from new.table4 A, table1 B where A.NEW.ID = B.IN.ID;
select
s1.names,
s2.names
from
IN_TABLE,
OUT_TABLE s1,s2
where
s1.IN_ID = s2.IN_ID;
(where s1 and s2 are alias)
or
select
left_table.name,
right_table.name as Report1
from
left_table,
right_table
where
left_table.IN_ID = right_table.IN_ID
please try and let us know if this solves your problem.
Also, see https://www.youtube.com/watch?v=KTvYHEntvn8 for more knowledge.
You need a join or a subquery.
Lets take a look of a join
Select
leftTable.Name,
rightTable.Name
from
leftTable
join rightTable
on leftTable.IN_ID = rightTable.In_ID
Edited: left to leftTable and right to rightTable
Related
I have a few tables with exact same schema, the only reason its separate is because they are huge.
So if i do a
select * from
(select * from ptable p1
union select * from ptable2 p2
.. and so on) pp
where pid=1234
, it will take really long time.
I like to write a one where i check ptable(s) for pid value of 1234, if it exist, then select the row from the right table.
How do i do that? pid is unique and will only exist in one table, it is also not in any sorted order.
I manage to solve my problem after some experiment, not sure if its the best way being an amateur but it works.
CREATE PROCEDURE 'sproc'(IN pQryID INT)
BEGIN
select count(*) as count into #rowCountp1 from p1 where pid=pQryID;
select count(*) as count into #rowCountp2 from p2 where pid=pQryID;
.. and so on for other similar tables ..
if #rowCountp1>0 THEN
(SELECT * from p1 where pid=pQryID);
elseif #rowCountp2>0 THEN
(SELECT ( from p2 where pid=pQryID);
end if;
END
I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.
SELECT prod_name,prod_desc,product_url,prod_price,img_name
FROM accu_product A, accu_product_imgs B
WHERE A.prod_id = B.prod_id
Is it possible to save the resulted table after use of join, and if yes, then what will the name of it. My query is as above.
CREATE TABLE whatever_you_want
SELECT
...
;
Eugen's answer will store the static result of your query. If you want a 'table' that updates as data in original tables change too, you can use views: http://dev.mysql.com/doc/refman/5.5/en/create-view.html
CREATE VIEW view_name
AS SELECT prod_name,prod_desc,product_url,prod_price,img_name
FROM accu_product A, accu_product_imgs B
WHERE A.prod_id = B.prod_id;
you can then query it as any other table
SELECT prod_name FROM view_name;
CREATE TABLE table_name
SELECT prod_name,prod_desc,product_url,prod_price,img_name
FROM accu_product A, accu_product_imgs B
WHERE A.prod_id = B.prod_id
try this:
I have a table A with one column named a, and a file "test.txt" contains:
111111AAAA
222222BBBB
3333DDDDDD
.....
The records in test.txt have the same type with "a" column.
How to select all from A except the records in "test.txt"?
Update:
I tried 3 ways and the results not equal. What a strange!
// 7073 records -- Using NOT IN
SELECT * from mt_users WHERE TERMINAL_NUMBER_1 NOT IN (SELECT TERMINAL_NUMBER FROM A);
// 7075 records -- Using NOT EXISTS
SELECT * from mt_users WHERE NOT EXISTS (SELECT 1 FROM A WHERE A.TERMINAL_NUMBER = mt_users.TERMINAL_NUMBER_1);
// 7075 records -- Using LEFT JOIN
SELECT * FROM mt_users m LEFT JOIN A a ON m.TERMINAL_NUMBER_1 = a.TERMINAL_NUMBER WHERE a.TERMINAL_NUMBER IS NULL;
Firstly put all records from file into the newTable and make sure that there are no additional spaces at the beginning or the end in each field.
select a from tableA t where not exists(select 1 from newTable n where n.a = t.a)
Step 1. Put the records from test.txt into a different table.
Step 2.
SELECT a from tableA WHERE a NOT EXISTS (SELECT a FROM newTable)
doing what aF wrote would be my first answer too. if you cant/do not want to do that try "NOT IN" like:
SELECT a FROM A WHERE a NOT IN(...)
You have to generate the content of the () in the code where you create your query
I have a table with a composite key composed of 2 columns, say Name and ID. I have some service that gets me the keys (name, id combination) of the rows to keep, the rest i need to delete. If it was with only 1 row , I could use
delete from table_name where name not in (list_of_valid_names)
but how do I make the query so that I can say something like
name not in (valid_names) and id not in(valid_ids)
// this wont work since they separately dont identity a unique record or will it?
Use mysql's special "multiple value" in syntax:
delete from table_name
where (name, id) not in (select name, id from some_table where some_condition);
If your list is a literal list, you can still use this approach:
delete from table_name
where (name, id) not in (select 'john', 1 union select 'sally', 2);
Actually, no I retract my comment about needing special juice or being stuck with (AND OR'ing all your options).
Since you have a list of values of what you want to retain, dump that into a temporary table. Then do a delete against the base table for what does not exist in the temporary table (left outer join). I suck at mysql syntax or I'd cobble together your query. Psuedocode is approximate
DELETE
B
FROM
BASE B
LEFT OUTER JOIN
#RETAIN R
ON R.key1 = B.key1
AND R.key2 = B.key
WHERE
R.key1 IS NULL
The NOT EXISTS version:
DELETE
b
FROM
BaseTable b
WHERE
NOT EXISTS
( SELECT
*
FROM
RetainTable r
WHERE
(r.key1, r.key2) = (b.key1, b.key2)
)