why occur this query "No database selected" - mariadb

mariadb version = 10.0.17-MariaDB-log
I am not in a position to choose a database at this time.
use dbname; --->> DO NOT USE THIS (if I use this query, it works well)
UPDATE dbname.t_co_sec_grade A INNER JOIN (
SELECT sec_id
, upper_sec_id
, sec_depth
, sec_name_kr
, IF(upper_sec_id = '111', '004', upper_sec_id) AS modify_upper_sec_id
, sec_depth - 1 AS modify_sec_depth
FROM dbname.t_co_sec_grade ABC INNER JOIN (SELECT #pv := '111') AS .initialisation
WHERE find_in_set(upper_sec_id, #pv) > 0
AND #pv := CONCAT(#pv, ',', sec_id)
) B ON A.sec_id = B.sec_id
SET A.upper_sec_id = B.modify_upper_sec_id, A.sec_depth = B.modify_sec_depth;
Why do I get "No database selected"?

It is an issue with earlier MariaDB/MySQL version caused by the INNER JOIN (SELECT #pv := '111'). You can update to the latest version which has this issue fixed.

If you do not include the "use dbname;" line you will indeed run into an issue with your update.
If you are attempting to write the query without the use, you can use a fully qualified line.
UPDATE [dbname].[schema].[table]

Related

EF Core - Count from a specific column

I almost have my EF Core query working... This is the SQL getting produced (notice the Count(*):
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(*) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
What I need is (have Count look at a specific column/table)
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(c.ID) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
Here is the EF Query that I'm using...
query = (from u in db.URLs
join ou in db.OwnerUrls on u.Key equals ou.ShortUrlKey into urlOwners
from subSet in urlOwners.DefaultIfEmpty()
join c in db.Clicks on u.Key equals c.ShortUrlKey into urlClicks
from subClicks in urlClicks.DefaultIfEmpty()
group subClicks by new { u.Key, u.Url, u.CreatedBy, u.CreatedOn } into g
select new ShortURL()
{
Key = g.Key.Key,
Url = g.Key.Url,
CreatedBy = g.Key.CreatedBy,
CreatedOn = g.Key.CreatedOn,
Clicks = g.Count()
});
I've tried changing the g.Count() to g.Select(x=>x.Id).Count() and that just causes EF Core to barf and complain about client side evaluation vs server side evaluation etc..
I should mention that the reason I'm joining the first model (OwnerUrls) is to support a where clause that I didn't include here...
Thanks!
I'm not a EF developer, but have worked with SQL Server for a while now. In SQL Server i would use COUNT(DISTINCT c.ID) to eliminate any duplicates you might get from JOINS.
If duplicates are impossible due to the model the COUNT(*) shoud be sufficient.
Maybe this might help:
https://entityframeworkcore.com/knowledge-base/51892585/linq-select-distinct-count-performed-in-memory

Update with subselect and where

The following code works fine in T-SQL, but not in JET SQL, in Access:
UPDATE Superliste_Temp
SET [Plan-TGrp-Spanne_Stfl1] =
(SELECT [Plan-TGrp-Spanne_Stfl1]
FROM Superliste_Temp
INNER JOIN dbo_Common_preferences
ON Superliste_Temp.Teil = dbo_Common_preferences.AktivesTeil)
WHERE [Teilegruppe] =
(SELECT [Teilegruppe]
FROM Superliste_Temp
INNER JOIN dbo_Common_preferences
ON Superliste_Temp.Teil = dbo_Common_preferences.AktivesTeil);
Why does it not work!?
I have a hard time looking at that SQL to figure out what it's trying to do, but I know that Jet SQL needs the SET statement after the JOINS, so my best guess is:
UPDATE Superliste_Temp, Superliste_Temp AS ST1
INNER JOIN dbo_Common_preferences AS pref1
ON ST1.Teil = pref1.AktivesTeil
SET Superliste_Temp.[Plan-TGrp-Spanne_Stfl1] = [ST1]![Plan-TGrp-Spanne_Stfl1]
WHERE (((Superliste_Temp.Teilegruppe)=[ST1]![Teilegruppe]));

sqlite manager lookup table [duplicate]

I tried :
UPDATE closure JOIN item ON ( item_id = id )
SET checked = 0
WHERE ancestor_id = 1
And:
UPDATE closure, item
SET checked = 0
WHERE ancestor_id = 1 AND item_id = id
Both works with MySQL, but those give me a syntax error in SQLite.
How can I make this UPDATE / JOIN works with SQLite version 3.5.9 ?
You can't. SQLite doesn't support JOINs in UPDATE statements.
But, you can probably do this with a subquery instead:
UPDATE closure SET checked = 0
WHERE item_id IN (SELECT id FROM item WHERE ancestor_id = 1);
Or something like that; it's not clear exactly what your schema is.
You can also use REPLACE then you can use selection with joins.
Like this:
REPLACE INTO closure
SELECT sel.col1,sel.col2,....,sel.checked --checked should correspond to column that you want to change
FROM (
SELECT *,0 as checked FROM closure LEFT JOIN item ON (item_id = id)
WHERE ancestor_id = 1) sel

Getting Data from SQL Statement that is Null along with Data that isn't from a Query String

Here is my SQL Statement :
SELECT machine,
fixedassets.[serial no],
[date]
FROM fixedassets
LEFT OUTER JOIN maintenancerecord
ON fixedassets.id = maintenancerecord.id
WHERE areaid = #AreaID
AND [record type] = 'Service History'
It's Currently being used by one of my tables in a GridView(ASP.NET) to show the Machines , Serial No of the Machines & the Date they was last serviced ( From Maintenance Record Where Record Type = Service History ) When I execute this through my Datasource attached to the GridView , it doesn't display the machines , for Example if the Area ID = 4 Then i know that at least 4 machines should appear even though they haven't yet received a Service History ( From a insert in a diff table but that's not the point it should still return them ) ... So my actual question is : Even If the machines have no Service History they should still be Returned in the table because that's what a Left Outer Join does right?
Any more code that needs to be provided can / will do , just ask in the comments.
Thanks you in advance!
[record type] should be check in the ON clause of the LEFT JOIN.
SELECT machine,
fixedassets.[serial no],
[date]
FROM fixedassets
LEFT OUTER JOIN maintenancerecord
ON fixedassets.id = maintenancerecord.id
AND maintenancerecord.[record type] = 'Service History'
WHERE areaid = #AreaID

How do I make an UPDATE while joining tables on SQLite?

I tried :
UPDATE closure JOIN item ON ( item_id = id )
SET checked = 0
WHERE ancestor_id = 1
And:
UPDATE closure, item
SET checked = 0
WHERE ancestor_id = 1 AND item_id = id
Both works with MySQL, but those give me a syntax error in SQLite.
How can I make this UPDATE / JOIN works with SQLite version 3.5.9 ?
You can't. SQLite doesn't support JOINs in UPDATE statements.
But, you can probably do this with a subquery instead:
UPDATE closure SET checked = 0
WHERE item_id IN (SELECT id FROM item WHERE ancestor_id = 1);
Or something like that; it's not clear exactly what your schema is.
You can also use REPLACE then you can use selection with joins.
Like this:
REPLACE INTO closure
SELECT sel.col1,sel.col2,....,sel.checked --checked should correspond to column that you want to change
FROM (
SELECT *,0 as checked FROM closure LEFT JOIN item ON (item_id = id)
WHERE ancestor_id = 1) sel

Resources