I have to like tables Order_Items and Order_Items_Archived which both tables are InnoDB. I would like to create a query that I can pull all the Items from both orders. I am pretty sure I would do this with a VIEW but I cant seem to find reference to just select all records that have the exact same column names and column types.
Example:
select sum(OrderItems_Amount)
from Order_Items_View
where OrderItems_OrderDate = '2017-10-01';
Sounds like you're looking for the union all operator:
CREATE OR REPLACE VIEW order_items_view AS
SELECT * FROM order_items
UNION ALL
SELECT * FROM order_items_archived
You would use a union all query. I would be inclined to add the source. List the columns that you want:
select col1, col2, col3, . . . , 0 as is_archive
from order_items
union all
selecct col1, col2, col3, . . ., 1
from order_items_archive;
You can put a create view statement before the query to turn it into a view.
Related
I have selected my data with;
SELECT * FROM item_temp WHERE name LIKE '%starter%' AND Inventory LIKE '600';
I want to duplicate my selected data (Not overwrite it) multiply "entry" of every item in the query by 10.
As an example, the "entry" of one item is: 51327.
I want to make a copy of the item with an entry of 513270.
I have tried a few different methods but they've all resulted in errors and I feel like I'm at a brick wall.
Thanks in advance.
Something like this:
select (it.entry * 10 + n) as entry, . . . -- the rest of the columns go here
from (select 0 as n union all select 1 union all . . . select 9) n cross join
item_temp it
where it.name LIKE '%starter%' AND it.Inventory LIKE '600' ;
Use the INSERT INTO syntax
INSERT INTO table_name
<your query with same column order as table_name>;
Another option is making the destination table ex-novo with select ... into statement
SELECT *
into new_table
FROM item_temp
WHERE name LIKE '%starter%'
AND Inventory LIKE '600';
Use INSERT INTO with a SELECT that does the multiplication you need. You will have to write all columns on for the inserting table.
INSERT INTO item_temp (
entry
-- , other columns
)
SELECT
T.entry * 10 AS entry
-- , other columns
FROM
item_temp T
WHERE
name LIKE '%starter%' AND
Inventory LIKE '600';
I have a table of 13 columns , out of those i have 5 columns that contains let say string data or VARCHAR data.
Now i have a string let say "abc".
I want to write a sql query to get all the rows that have this "abc" string in those 5 columns. "abc" can be the data of the column of part of the data of the column.
I used LIKE function
SELECT * FROM table WHERE col1 OR col2 OR col3 OR col4 OR col5 LIKE '%abc%';
but it didnt worked n got back all the rows.
I dont know how use MATCH function either, I m nt good in sql. so can anyone help.
You can specify condition multiple times:
SELECT *
FROM table
WHERE col1 LIKE '%abc%'
OR col2 LIKE '%abc%'
OR col3 LIKE '%abc%'
OR col4 LIKE '%abc%'
OR col5 LIKE '%abc%';
This will be really slow because you have multiple OR and non-SARGable condition.
Alternatively:
SELECT *
FROM table
WHERE CONCAT_WS('^', col1,col2,col3,col4,col5) LIKE '%abc%';
Using MATCH (preferred solution that utilizes full-text index):
SELECT *
FROM table
WHERE MATCH(col1, col2,col3,col4, col5) AGAINST ('abc');
SqlFiddleDemo
Keep in mind that to use MATCH you need to create index first:
ALTER TABLE tab ADD FULLTEXT ft_index_name (col1,col2,col3,col4,col5);
If SQL query contains all fields what I need it will be very large.
How in SQL query I can select field from F3 to F 100 and from F150 to F200?
SELECT F3 to F100, F150 to F200 FROM database;
It is possible or not???
Tables structure change is not available
You have to :
1- manually select all columns . Or
2- do
Select * from database
And then just fetch the columns you need.
There are no shortcuts for this, you will have to list the fields needed one way or another. If the fields being selected are always the same, you should create a view for that as:
CREATE VIEW SomeView AS
SELECT
F3,
...
F100
FROM
SomeTable
and then select like:
SELECT * FROM SomeView
But again, you will have to list the fields at least once.
SELECT F3 to F100, F150 to F200 FROM database;
this query can not possible..
you must have to specify all the columns name
like select F1,f2,f3 from database;
You can't.
But if it's not possible to modify your table structure to fix the database design issue, you can use an SQL query to generate the MySQL query:
SELECT CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM `your table`')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your schema' and TABLE_NAME = 'your table'
GROUP BY TABLE_NAME
Add a filter in WHERE to select only the desired fields.
I have following query:
SELECT `mmetal`.`id`, `mmetal`.`name`, `steelmarks`.`EN`, `steelmarks`.`DIN` FROM `mmetal` LEFT JOIN `steelmarks` ON `mmetal`.`id`=`steelmarks`.`id` WHERE REPLACE(REPLACE(REPLACE(REPLACE(`name`,' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'
(REPLACE - replacing of " ", "(", ")", "-" in name column)
1) steelmarks table have about 15 columns - I need to replace
`mmetal`.`id`, `mmetal`.`name`, `steelmarks`.`EN`, `steelmarks`.`DIN`,`steelmarks`.`column3`,...,...,...`
with something like
`mmetal`.`id`, `mmetal`.`name`, `steelmarks`.*
but it not works
2) I wish to use LIKE function with REPLACE to all selected columns except id columns in both tables, not only "name". Something like:
WHERE REPLACE(REPLACE(REPLACE(REPLACE(ALL COLUMNS,' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'
now i need to use
WHERE REPLACE...column1 LIKE %something% OR REPLACE...column2 LIKE %something% OR REPLACE...column3 LIKE %something% OR ...
Do you have any suggestion for my questions, please?
The replace and like operators only operate on one column at a time. The following might do what you want, but you still have to list all the columns:
REPLACE(REPLACE(REPLACE(REPLACE(concat_ws('|', col1, col2, col3, . . . ),' ',''),'\)',''),'\(',''),'-','') LIKE '%something%'
This concatenates the values together with a separator.
Alternatively, you could query INFORMATION_SCHEMA.COLUMNS with something like:
select concat(replace(<YOUR EXPRESSION HERE>, 'col1', c.column_name), ' and')
from information_schema.columns c
where table_name = YOURTABLEHERE
Then use the results to construct your query.
I have lot of MySQL tables with the same column name. So Im looking for PDO or SQL hack for SELECT * FROM more tables - which will return table names in result sets.
Example:
'SELECT * FROM table0, table1';
Where both tables has 'name' column. But FETCH_ASSOC result returns only one 'name' - the last one.
Result:
echo $result["name"];
Wanted result:
echo $result["table0.name"];
echo $result["table1.name"];
...
Note that
I cannot rename DB columns to be unique
I cannot manualy create alias for each columns (lot of tables/columns)
I want name in result set not numbers like FETCH_NUM
Any ideas?
Thanks!
Hack doesn't exist, you have to create aliases.
You said that you don't want to alias all columns because there are too many, but have you considered only aliasing the ones that give you problems?
SELECT
*,
table0.name AS t0name,
table1.name AS t1name
FROM table0 JOIN table1 ON ...