Suppose I have a long query in sqlite3 command line as follows:
select col1, col2, col3 from table1
join table2
on table2.table1_id=table1.id
join table3
on table3.table2_id=table_2.id
where col1 like "%some text%";
Now I would like to try this query for different wildcards like "%some text 2%" and "%some text 3%".
I only know of two options here:
rewriting the query multiple times in the command line (a pain in the butt)
committing the whole thing to a script and executing with .read test.sql
Is there another way? Does there exist some function-like concept in sqlite3 which applies here? Ideally I would like to define the "function" and call it from the command line.
SQLite does not support functions.
What you could do, to save you from retyping the main part of the query, is create a view:
create view MyView as
select col1, col2, col3 from table1
join table2
on table2.table1_id=table1.id
join table3
on table3.table2_id=table_2.id
Then you can add the WHERE clause whenever you want to execute it:
SELECT * FROM MyView
WHERE col1 LIKE ?;
Related
I would like to know how to write sql query to search string in all columns in a table.
i.e in single where condition
I have column1, column2,... column50 fields in a table
Right now am using query like
select * from tblist where column1 like '%searchstr%'OR column2 like '%searchstr%' OR ....it goes on
Is there anyway to write sql query to search string in all columns
In the case of your exact query as given, we can try using IN:
SELECT *
FROM tblist
WHERE 'searchstr' IN (column1, column2, ...);
If you really need to use LIKE here, then there is no real shortcut available.
you could save on the grunt work by using the metadata tables to generate your query and then run it. Eg:
select concat('%searchstr% like ',COLUMN_NAME,' OR ')
from information_schema.columns t
where table_name='t' /*change to the table name*/
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.
I want to write column headers into my csv files, and cannot figure out how, using select .. into syntax.
I've visited this page, as well as looking at some SO posts on the subject. I am wondering if MySQL's select .. into provides a feature to write the column headers or if there's another way to do that, while still writing a .csv file. A plain select at the command line does write the column headers.
Ugly, but solves your proble,
select * INTO OUTFILE from (
select 'col1', 'col2', 'col3'
UNION ALL
select col1, col2, col3 from table_name) as t
I would like to rename the columns in the results of a SELECT expression. Of course, the following doesn't work:
SELECT * AS foobar_* FROM `foobar`
As I'm new to SQL, I think I'm just missing a concept, tool, or keyword that would lead to the answer. A hint in the right direction would be appreciated. Thanks!
UPDATE
I'm looking for a generic way to do this, and MySQL-specific techniques are absolutely fine.
In short, I'm writing a tool that "exports" the results of MySQL queries to Google Spreadsheets (via the Google Data API). Some queries are joins, so to make columns unique I wanted to prefix all column names with their respective table names.
You can alias the column names one by one, like so
SELECT col1 as `MyNameForCol1`, col2 as `MyNameForCol2`
FROM `foobar`
Edit You can access INFORMATION_SCHEMA.COLUMNS directly to mangle a new alias like so. However, how you fit this into a query is beyond my MySql skills :(
select CONCAT('Foobar_', COLUMN_NAME)
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'Foobar'
you have to rename each column
SELECT col1 as MyCol1,
col2 as MyCol2,
.......
FROM `foobar`
select column1 as xyz,
column2 as pqr,
.....
from TableName;
I have a query that looks at partial matches within two mysql tables to make a join:
SELECT table1.column, table2.column FROM table1, table2 WHERE table1.val LIKE table2.val
This works fine as a join, but... some of the values in table2 are actually substrings of the values in table one—specifically they're urls. So, table1.val might equal http://google.com while table2.val = google.com.
How do I use the '%' operator around the table2 val to make this comparison.
Thanks in advance!
Like this:
... WHERE table1.val LIKE CONCAT('%', table2.val, '%')
Note that this will not perform as well as table1.val = table2.val, as it must now search all rows in table2.
Q: How do I use the '%' operator around the table2 val to make this
comparison.
A: You don't :)
You can specify a column by name (e.g. "mycolumn"), by fully qualified name (e.g. "mytable.myname") or by ordinal (e.g. "1").