I have problems with sqlite3 and "num_rows". I cannot ask for num_rows of a result like mysql.Now i wan't to automate my SELECT query to ask for the rowcount.
Is there a way to replace all between two patterns?
Like
SELECT date,name,etc FROM myTable WHERE gender=1
// to
SELECT count(*) FROM myTable WHERE gender=1
I must replace all between SELECT and FROM with count(*).
Can i do this this preg_replace?
For your example provided it's simple, but I guess it won't work for all query syntaxes. But have it a try:
PHP code (don't know language you do use):
$count_sql = preg_replace('/^(SELECT)\\s+.*?\\s+(FROM.*)$/s', '$1 count(*) $2', $select_sql);
But I guess, there is a more elegant way to find the num_rows thing. You could use a subquery, like:
SELECT COUNT(*) FROM ( *** original query here *** )
It's not very efficient, but you don't have to deal with the query syntax.
Related
I would like to be able to SELECT a NULL value in my SELECT list so I can use it later in a UNION (for example). When I try SELECT NULL AS my_null_value FROM some_table it gives me a syntax error. I can work around it by doing the following.
SELECT CASE WHEN 1=1 THEN NULL END AS my_null_value
FROM some_table
Is there a cleaner way to accomplish this? I feel like I have done this in less characters and in a simpler manner in the past than using a CASE statement.
I ending up finding it.
A simpler (or more shorthand) way to accomplish this is to use the following:
SELECT NULLIF(0,0) AS my_null_value
FROM some_table
I have a source database & a target database. I want to execute .append command against the target database but source tables are in the source database. I know database('SourceDB') function we can use from the target database but I don't know how to use this in combination of search *. So what I need will look something like this:-
.set target_table <| search database("SourceDB").* | ..some logic
This is not working. Is there any workaround for this?
would something like this work?
.set target_table <| union database("SourceDB").* | distinct colA, colB, colC.
BTW, syntax-wise - using search (if you still require that) could look like:
search in (database("SourceDB").*) "something you're searching for"
What I want to achieve (if it is possible from SQL Developer) is that when I execute the script it do the following:
Run a SELECT statement that will return a list of IDs. Approx 270 records.
I need to use each of those IDs individually in another SELECT statement (in the WHERE clause) which will return some records. A few of this could result in over 17,000 records and some can be one.
Then each result from the second SELECT I want it to be exported to an excel or csv file into a folder at my pc.
I have both 'Select' ready but I don't know how to loop over the results of the first to grab each ID and use it in the second one. Also I don't know how to export automatically from the code.
You can use GROUP BY clause.
read more here:
http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html
If the first SELECT returns you IDs only, and that's all you need for your second SELECT, just use IN clause providing your first SELECT query for the second one.
Example:
-- Your second select
SELECT
col1
,col2
,col3
,col4
FROM
second_table
WHERE
some_foreign_id IN (
-- Your first select
SELECT
id
FROM
first_table
WHERE
some_conditions...
)
In my opinion don't use PLSQL for this. In PLSQL the only way you can get an output of this by using a REFCURSOR (unless you dont use UTIL File package to do it). A simple SELECT WITH JOIN condition will suffice your requirement.
Test illustration
SELECT A.* FROM TABLE_A A, TABLE_B
WHERE A.COMMON_COLUMN = B.COMMON_COLUMN;
Hope this helped you in same way
I am trying to combine the two into a single statment, I would even settle for two seperate statements..... I know it must be possible, but how?
this is what I have tried:
DELETE FROM myTable WHERE myValue LIKE 'findme%';
and:
DELETE FROM myTable WHERE EXISTS (SELECT * FROM myTable WHERE myValue LIKE 'findme%');
I get an error for the second statement saying something like, you can only have a single result for a LIKE statement with another statement...
If this statement returns any rows
select * from mytable where myvalue like 'findme%';
then replacing "select *" with "delete" should delete them.
delete from mytable where myvalue like 'findme%';
That should work with any SQL database, as long as you have sufficient permissions.
Your first statement should work. Try without the final semicolon maybe?
The second one is not logical. Your subquery is not correlated to the first one and you cannot scan a table which is being modified. Actually what I would expect from this query, if it would ever run, is that all rows are deleted if there is a single row where your column matches…
If you wonder why the solutions with the IN clause work and not the EXISTS, it is because the EXISTS condition is evaluated for each row, whereas the IN set is evaluated once.
This works to me:
DELETE FROM myTable WHERE id IN (SELECT id FROM myTable WHERE myValue LIKE 'findme%');
Have you tried this?
DELETE FROM myTable WHERE myValue IN (SELECT myValue FROM myTable WHERE myValue LIKE 'findme%');
DELETE FROM `table_name` WHERE `column_name` LIKE 'value%'
Maybe i should do this in C# but i have more then one row with linkId X. I would like to remove it but i am unsure how. In code i could just use a foreach from 0 to n and remove any found rows with a greater (or !=) id but thats in code. Is there a less difficult way of doing it using sqlite?
Assuming the table's name is tableName and there is a primary key field named id, the following sql would do it. I think the following SQL query is general enough and should be able to be executed under any database engine.
delete from tableName
where id not in (
select min(id) from tableName
group by linkId
)