save table after use mysql JOIN - mysql

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:

Related

i am just tried to create an sql query but not getting

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

Update table values from another table in sql

I have a temporary table havinf 2 colums and i need to update this data into another table
Can Some one please help me with this
You want to use join, something like this:
update othertable t join
temporarytable tt
on t.clientid = tt.clientid
set t.checklist = tt.checklist;

MySQL JOIN INSERT

I have tree table
a (a_col1, a_col12, a_col3)
b (b_col1, b_col12, b_col3)
c (c_col1, c_col12, c_col3)
I want to write the b.b_col3 to c.c_col3
where a.a_col1 equals to b.b_col12.
What am I doing wrong ?
INSERT INTO c(c_col3)
SELECT a.a_col1, b.b_col12
FROM a LEFT JOIN b
ON
a.a_col1 = b.b_col12;
You are trying to insert 2 columns value in single column, use something like below-
INSERT INTO c(c_col2,c_col3) SELECT a.a_col1, b.b_col12 FROM a LEFT JOIN b ON a.a_col1 = b.b_col12;
You can not do both stuff with one query. You cannot INSERT and SELECT at the same time. Try first selecting and then inserting if it is possible.

Select all from table except the records from a file

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

INSERTing rows that is not in a table

I'm using this query but it is really really slow
INSERT INTO a (b,c,d,e,f,g,h,i) SELECT b,c,d,e,f,g,h,i FROM z WHERE b NOT IN (SELECT b FROM a)
What is does is find all records where b is not in table "a" from table z and importing it into table a.
Its really really slow and keeps timing. Is there away to make it quicker?
Thank-you
BigThings
P.s.
Make the b column unique, then INSERT with the IGNORE option, so:
INSERT IGNORE INTO a (b,c,d,e,f,g,h,i)
SELECT b,c,d,e,f,g,h,i FROM z
INSERT INTO a (b,c,d,e,f,g,h,i) SELECT a.b,a.c,a.d,a.e,a.f,a.g,a.h,a.i FROM z,a WHERE z.b != a.b
INSERT INTO a (b,c,d,e,f,g,h,i) SELECT b,c,d,e,f,g,h,i FROM z WHERE NOT EXISTS (SELECT 1 FROM a WHERE z.b = a.b)
Use this simple trick:
INSERT INTO a (b,c,d,e,f,g,h,i)
SELECT b,c,d,e,f,g,h,i
FROM z
LEFT JOIN a on a.b = z.b
WHERE a.b IS NULL;
You'll only get a row when there isn't a matching row in b, and the query will be able to use indexes effectively.