I'm working with a LAMP server (it's a bananapi) with MYSQL 5.6 server installed on it. I have a table with several fields.
From mysql command line, if I run:
select * from tbl_name where field1=val1 and field2=val2
it returns N results.
If I run instead:
select COUNT(*) from tbl_name where field1=val1 and field2=val2
it returns 0..
In particular, field1 and field2 are foreign key values to other two separate tables. All tables are InnoDB.
It's a bug???
are you enabled fulltextsearch on the table?
i have same problem and fixed by this trik!
select count(*) from (select * from tbl_name where field1=val1 and field2=val2) as t
Related
I'm trying to convert my SQlite code to Mariadb but i'm stuck with a query for a crossed table between two another tables
The purpose of the table is to add ID from the other table but only if they don't exists
I have two tables
Table COMPUTERS
ID(primary) / NETBIOS(unique) / IP / SOFTWARE_STAT / COPY_STAT / AV_STAT
ex : 1 PC1 192.168.1.1 KO KO 0
Table SOFTWARES
ID(primary) / NAME(unique)
ex : 1 ADOBE
And the cross table
Table INSTALL
ID (primary)/ COMPUTER_ID / SOFTWARE_ID / FAIL
1 1 1 0
My SQLITE code below is working
INSERT OR IGNORE INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1'),
(SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
WHERE NOT EXISTS
(SELECT
COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT ID FROM COMPUTERS WHERE IP = '192.168.1.1')
AND
SOFTWARE_ID = (SELECT ID FROM SOFTWARES WHERE NOM = 'ADOBE')
)
I've tried that
INSERT INTO INSTALL (COMPUTER_ID,SOFTWARE_ID)
SELECT
(SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1'),
(SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE')
WHERE NOT EXISTS
(SELECT COMPUTER_ID,SOFTWARE_ID FROM INSTALL
WHERE
COMPUTER_ID = (SELECT RowID FROM COMPUTERS WHERE IP='192.168.1.1')
AND
SOFTWARE_ID = (SELECT RowID FROM SOFTWARES WHERE NOM='ADOBE'));
Without success
Someone have an idea ? Thanks in advance.
INSERT OR IGNORE --> INSERT IGNORE
However, you have a redundancy: (1) IGNORE, and (2) NOT EXISTS. That is, you both verify that it is not a dup and you ignore errors if it is.
Otherwise, the Sqlite code should work as is in MariaDB. What went wrong? Did you get an error?
MariaDB has no RowID.
I know this isn't a specific bit of code or problem, but I am having trouble with a very similar issue to the person asking this (except theirs is for SQL Server): Combining INSERT INTO and WITH/CTE ...and I can't seem to find it out there on any SAP HANA help forums etc. so thought there may be an expert on here who can just give me a simple yes or no answer.
The SQL statement I am using contains multiple CTEs, but when I try to insert it tells me there is a Syntax error around the word INSERT. It is definitely laid out exactly the same as in the question I've linked above (spent hours checking), and I can post code samples if necessary but I simply want to know whether it is supported first! Thanks
Short answer:
No, CTEs are not supported for INSERT/UPDATE statements.
Longer answer:
SQLScript's INSERT/UPDATE commands are actually "borrowed" SQL commands as the documentation explains.
Checking the documentation for SQL INSERT we find that it supports a subquery as a source of values.
The subquery term is defined as part of the SQL SELECT statement. Checking the documentation for SELECT shows that <subquery> and <with_clause> are different, non-overlapping terms.
This means, that CTEs cannot be used in subqueries and therefore not be part of the subqueries used in INSERT/UPDATE commands.
You can, however, use SQLScript table variables in INSERT statements in your SQLScript blocks, which is very similar to CTEs:
DO BEGIN
te_a := SELECT 10, 'xyz' as VAL from dummy;
te_b := SELECT 20, 'abc' as VAL from dummy;
te_all := SELECT * from :te_a
UNION ALL SELECT * from :te_b;
INSERT INTO VALS
(SELECT * from :te_all);
END;
You can convert the CTE into a Sub-Select statement in many cases
You can use following
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from (
-- CTE Expression as subselect
select * from city
-- end (CTE)
) cte
Instead of using following valid CTE command combined with INSERT (on SQL Server)
with cte as (
select * from city
)
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from cte
SAP HANA includes this posibility, the order of the code is different than SQL Server:
INSERT INTO EXAMPLE (ID)
WITH cte1 AS (SELECT 1 AS ID FROM DUMMY)
SELECT ID FROM cte1;
Kindly help me writing below query in openquery.Thanks in advance
INSERT INTO Tablename
SELECT * FROM tablename1 WHERE insertionorderid IN (
SELECT orderid FROM temp_table2)
If you are trying to insert into SQL Server the syntax would be
INSERT INTO dbo.[YOURTABLE] SELECT * FROM [MATCHING_TABLE] WHERE CLAUSE
The caveat here is that the two tables must have identical schemas or you will have to explicitly define the columns. Also this will not work properly for tables with identity columns
In my project, I have a database in SQL which was working fine. But now I have to make the application support oracle db too.
Some limitations I found out was that in Oracle, there is no bit field and the table name cannot be greater than 30 char. Is there any other limitation that I need to keep in mind.
Any suggestion from past experience will be helpful.
If I recall correctly from my earlier Oracle days:
there's no IDENTITY column specification in Oracle (you need to use sequences instead)
you cannot simply return a SELECT (columns) from a stored procedure (you need to use REF CURSOR)
of course, all stored procs/funcs are different (Oracle's PL/SQL is not the same as T-SQL)
The SQL ISNULL counterpart in Oracle is NVL
select ISNULL(col, 0)...
select NVL(col, 0)...
You will also struggle if you attempt to select without a from in Oracle. Use dual:
select 'Hello' from DUAL
Bear in mind also, that in Oracle there is the distinction between PL/SQL (Procedural SQL) and pure SQL. They are two distinct and separate languages, that are commonly combined.
Varchar in Oracle Databases called
varchar2 is limited to 4000
characters
Oracles concept of temporary tables is different, they have a global redefined structure
by default sort order and string compare is case-sensitive
When you add a column to a select *
Select * from table_1 order by id;
you must prefix the * by the table_name or an alias
Select
(row_number() over (order by id)) rn,
t.*
from table_1 t
order by id;
Oracle doesn't distinguish between null and '' (empty string). For insert and update you ca use '', but to query you must use null
create table t1 (
id NUMBER(10),
val varchar2(20)
);
Insert into t1 values (1, '');
Insert into t1 values (2, null);
Select * from t1 where stringval = 0; -- correct but empty
Select * from t1 where stringval is null; -- returns both rows
ORACLE do not support TOP clause. Instead of TOP you can use ROWNUM.
SQL Server: TOP (Transact-SQL)
SELECT TOP 3 * FROM CUSTOMERS
ORACLE: ROWNUM Pseudocolumn
SELECT * FROM CUSTOMERS WHERE ROWNUM <= 3
Does sqlite support the SELECT INTO statement?
Actually I am trying to save the data in table1 into table2 as a backup of my database before modifying the data.
When I try using the SELECT INTO statement:
SELECT * INTO equipments_backup FROM equipments;
I get a syntax error:
"Last Error Message:near "INTO":syntax
error".
Instead of
SELECT * INTO equipments_backup FROM equipments
try
CREATE TABLE equipments_backup AS SELECT * FROM equipments
sqlite does not support SELECT INTO.
You can probably use this form instead:
INSERT INTO equipments_backup SELECT * FROM equipments;
SQlite didn't have INSERT INTO syntax.
In 2019, I use TEMPORARY keyword to create temporary table and INSERT data to temp table:
CREATE TEMPORARY TABLE equipments_backup(field1 TEXT, field2 REAL)
INSERT INTO equipments_backup SELECT field1, field2 FROM equipments