I've been trying to show a specific type of result in SQL but haven't been able to. I guess it's possible, but can't figure out how.
I've read this article show only those columns which have data value but couldn't solve my problem.
I have 4 tables: sparametros (holds parameters codes and descriptions), sparametrosnumericos (holds numeric parameters values and their code), sparametrostexto (holds text parameters and their values), sparametrosmemo (holds memo type parameters)
All of them can be joined by their parameter code, however, a parameter code is unique in the sense that for example, given a code, let's say 1210, and let's suppose it's a text type parameter, then that code doesn't exist in numeric nor in memo parameters either. However it exists in the general sparametros table. In other words, sparametros holds all parameters, and the other tables represent sub sets of that main set.
I've tried using left join, but couldn't get results.
This is what I have so far:
SELECT P.SPar00Id, P.SPar00Descripcion,
IF NOT ISNULL(N.SPar00NumValor) THEN
N.SPar00NumValor
ELSEIF NOT ISNULL(T.SPar00TextoValor) THEN
T.SPar00TextoValor
ELSE
M.SPar00MemoValor
FROM sparametros p
LEFT JOIN sparametrosnumericos N ON N.SPar00NumId = P.SPar00Id
LEFT JOIN sparametrostexto T ON T.SPar00TextoId = P.SPar00Id
LEFT JOIN sparametrosmemo M ON M.SPar00MemoId = P.SPar00Id
WHERE P.SIns00Id = 1 AND
N.SIns00Id = 1 AND
T.SIns00Id = 1 AND
M.SIns00Id = 1;
I'm using MySQL now (with the Navicat client), but also need to be able to get the same results in SQL Server.
The response I'm getting when executing this request is:
"[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT ISNULL(N.SPar00NumValor) THEN "
Try this:
SELECT P.SPar00Id, P.SPar00Descripcion,
(CASE WHEN N.SPar00NumValor IS NOT NULL THEN N.SPar00NumValor
WHEN T.SPar00TextoValor IS NOT NULL THEN T.SPar00TextoValor
ELSE M.SPar00MemoValor
END) colName
FROM sparametros p
LEFT JOIN sparametrosnumericos N ON N.SPar00NumId = P.SPar00Id
LEFT JOIN sparametrostexto T ON T.SPar00TextoId = P.SPar00Id
LEFT JOIN sparametrosmemo M ON M.SPar00MemoId = P.SPar00Id
WHERE P.SIns00Id = 1 AND N.SIns00Id = 1 AND T.SIns00Id = 1 AND M.SIns00Id = 1;
Related
I'm trying to build an SQL-Query with MySQL-Version 5.7.27.
I'm trying to make multiple left joins to the same table. At some point i get an error, that tells me, a column of the original table is unknown.
I tried finding the syntax error in my query, but there can't seem to be an obvious one. I tried out some online SQL-validators. They keep telling me, my query is valid.
After that i tried picking apart the query, to find out, what exactly i am working with before the left join that makes my query unable to function. Doing that actually showed me a result, that was including the column the error was telling me, that is missing.
So maybe it is not a syntax error, but MySQL is validating things in a order different to what i know, trying to pick the "missing" column out of somewhere it actually doesn't exist.
Changing the order of left joins actually solves the problem. I can't really do that, tho. The SQL is being generated by some functions. One for every part of the SQL. One for the "SELECT", one for the "FROM" and so on. So a working thing is joining the minimized versions of the "additional_agreement_fields" table before the other joins. Sadly the function for the "FROM" part is written ... in a very interesting way. So my best bet is adding the "additional_agreement_fields" joins after the function that creates the FROM.
That attempt generated the SQL below.
I get following error:
Error Code: 1054. Unknown column 'customers.id' in 'on clause'
So:
- The parts of the query are working on their own
- Execution until the point of the error holds a table that has the
"missing column"
- Changing the order of the joins solves the problem, but i can't just
do that and i want to know, why it doesn't work in this order
- Online-validators can't find any syntax errors
- Logically i am just adding columns to a table that are not ambiguous
and not removing any. Yet something becomes unable to be found
SELECT DISTINCT
(customers.id),
companies.company_name AS 'Kunde -> Firmenname',
vcpt_mail.tcom_value AS 'Kontakt -> TCom -> Email',
v_contact_people.id AS 'Kontakt -> ID',
v_contact_people.last_name AS 'Kontakt -> Nachname',
A0.value AS 'Kunde -> Zusatzvereinbarung -> TestfeldB'
FROM
customers
LEFT JOIN
customer_types ON customer_types.id =
customers.customer_type_id,
v_customer_owners,
companies
LEFT JOIN
(v_contact_people
LEFT JOIN v_cp_tcoms AS vcpt_mail ON
vcpt_mail.contact_person_id = v_contact_people.id
AND vcpt_mail.ttid = 3
AND (vcpt_mail.type_label_access_path = '/Global'
OR vcpt_mail.type_label_access_path LIKE '/Global%')) ON
v_contact_people.company_id = companies.id
AND v_contact_people.access_path LIKE '/Global%'
LEFT JOIN
(SELECT
*
FROM
additional_agreement_fields
WHERE
name = 'TestfeldB' AND value = '5') AS A0 ON customers.id
= A0.customer_id
LEFT JOIN
(SELECT
*
FROM
additional_agreement_fields
WHERE
name = 'TestfeldC' AND value = '6') AS A1 ON customers.id
= A1.customer_id
WHERE
v_customer_owners.customer_id = customers.id
AND v_customer_owners.path LIKE '/Global%'
AND customers.company_id = companies.id
AND companies.deleted_at IS NULL
AND customers.deleted_at IS NULL
AND (A0.value = '5' AND A0.name = 'TestfeldB'
OR A1.value = '6' AND A1.name = 'TestfeldC');
The expected result is a not empty table with 2 customers in it, i prepared to find and that are found, changing the join order.
The result is an interrupted query with an error code.
Error Code: 1054. Unknown column 'customers.id' in 'on clause'
For a projet, I retrieved the database from a client. This database contains tables that have attributes composed by several words that are concatenated separated by a point. exemple :
table A : id, number.client , global.score
table B : id, favorite.style
So when I do a sql request on this tables to filter to filter the results according to number.client for exemple, I have an error returned by MySQL.
For exemple I have this request :
SELECT * FROM A
INNER JOIN B
ON (A.ref_track = B.id)
INNER JOIN C
ON (C.id = B.ref_plannode)
WHERE (B.id= 1)
AND (A.number.client > 50)
ORDER BY A.id DESC
When I run this request I get this error :
MySQL replied:
#1064 - Syntax error on A.number.client > 50
I think that MySql don't failed when we have an attribute composed by servel words separated by point ( like number.client). So what is the solution ??
For inforamtion this database is out of my responsibility, I got it from a client!!.
You have to quote the columns names with "`"
SELECT *
FROM A
JOIN B
ON A.ref_track = B.id
JOIN C
ON C.id = B.ref_plannode
WHERE B.id= 1
AND A.`number.client` > 50
ORDER BY A.id DESC
Edit
Working example
Is the same case that white spaces, you can use this:
SELECT * FROM A
INNER JOIN B
ON (A.ref_track = B.id)
INNER JOIN C
ON (C.id = B.ref_plannode)
WHERE (B.id= 1)
AND (A.`number.client` > 50)
ORDER BY A.id DESC
The MySQL can use diferent quotes or braquets, depends of configuration.
The MySQL server can operate in different SQL modes, and can apply these modes differently for different clients, depending on the value of the sql_mode system variable.
You can view the configuration with this command:
SELECT ##GLOBAL.sql_mode;
SELECT ##SESSION.sql_mode;
And you can know more about the configuration of mysql here https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi_quotes
Try to quote the column name putting it between square brackets.
So
AND (A.[number.client] > 50)
I have a query that is returning two different results from two MySql servers i'm running.
This is the query (generated by Entity Framework, I run it directly in MySql to ensure the results are coming back from each server differently):
SELECT
CASE WHEN Project1.C1 IS NULL THEN '5' ELSE Project1.data_entered END AS C1
FROM ( SELECT 1 AS X) AS SingleRowTable1
LEFT OUTER JOIN (SELECT
Extent1.data_entered,
1 AS C1
FROM rta_option_data AS Extent1
INNER JOIN rta_option_field AS Extent2 ON Extent1.rta_option_field_id = Extent2.rta_option_field_id
WHERE (Extent1.customer_id = 1546) AND (Extent2.Name = 'txtAdverseDelay') ) AS Project1 ON 1 = 1
Server A is running MySql 5.5.28 and returns 5 which is the correct and expected value.
Server B is running MySql 5.7.12-log and returns null which is the incorrect and unexpected value.
I'm at a complete loss of how to resolve this, I was planning a server upgrade this weekend but i'm super worried about other similar queries that could be impacted by this.
Additional notes:
They are both using the exact same data.
When I run the inner query on both servers they return the same results data_entered = null and C1 = null. This is the inner query i'm talking about:
SELECT
Extent1.data_entered,
1 AS C1
FROM rta_option_data AS Extent1
INNER JOIN rta_option_field AS Extent2 ON Extent1.rta_option_field_id = Extent2.rta_option_field_id
WHERE (Extent1.customer_id = 1546) AND (Extent2.Name = 'txtAdverseDelay')
Would appreciate any guidance/advice on how to possibly fix this - not sure if there is a setting in MySql or what.
I have branch table , inventory table and item table. I want to retrieve item id, item name , qty (quantity) and branch name (branch_add).
SELECT tbl_item.item_name ,
tbl_inventory. tbl_item_item_ID , tbl_inventory.qty , tbl_branch.branch_add
FROM tbl_item,tbl_inventory ,tbl_branch
WHERE (tbl_inventory.tbl_item_item_ID = tbl_item.item_ID) JOIN (tbl_branch.branch_ID=tbl_inventory`.tbl_branch_branch_ID)
That is the code which I have wrote but it gives bellow error.
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'join item_ID.tbl_item =
tbl_item_item_ID.tbl_inventory' at line 3
Could anyone please help me to solve this. And table will displayed on attached png.
You have a missing AND between your WHERE conditions:
select
tbl_branch_branch_ID.tbl_inventory
, item_name
from tbl_inventory
, tbl_item
, tbl_branch
where
branch_ID.tbl_branch=tbl_branch_branch_ID.tbl_i
and item_ID.tbl_item = tbl_item_item_ID.tbl_inventory;
Also, your JOIN conditions are a little bit off.
I think this is the query you're actually looking for:
select
t_inv.*
, t_itm.item_name
from tbl_inventory t_inv
inner join tbl_item t_itm on t_inv.t_item_item_id = t_itm.item_id
inner join tbl_branch t_br on t_br.branch_id = t_inv.tbl_branch_branch_id
Using explicit JOINs is much better and clearer for reading the code and also can result in improved code performance.
SELECT
tbl_item.item_name,
tbl_inventory.tbl_item_item_ID,
tbl_inventory.qty,
tbl_branch.branch_add
FROM
tbl_item,
tbl_inventory,
tbl_branch
WHERE
(
inner join tbl_item ON tbl_inventory.tbl_item_item_ID = tbl_item.item_ID
inner join branch_ID ON tbl_branch.branch_ID = tbl_inventory.tbl_branch_branch_ID
);
The above query gives syntax error.
I'm getting a syntax error in MySQL query. Is MySQL and SQL server work differently? Can anyone suggest, what is wrong and where ?
select b.component, d.matter, d.bug, d.timestamp, d.os
from bugs.profiles p, ops_reports.BPR_TAG_DATA d
left join (Select * from bugs where product='test') b
on d.bug=b.bug_id
where d.tagid = 6
and timestamp between "2014-04-21" and "2014-04-24"
and login_name like 'test'
and p.userid = d.user
Error Message 24/04/2014 23:14:10 0:00:00.037 MySQL Database Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Select * from bugs where product='Conversions') as b
on (d.bu 1 0
You should not mix implicit and explicit joins. A simple rule: just don't use commas in the from clause.
select b.component, d.matter, d.bug, d.timestamp, d.os
from ops_reports.BPR_TAG_DATA d left join
bugs b
on b.product = 'test' and d.bug = b.bug_id left join
bugs.profiles p
on p.userid = d.user
where d.tagid = 6 and
timestamp between '2014-04-21' and '2014-04-24' and
login_name like 'test';
I also removed the subquery, moving the condition to the on clause. This makes the query more efficient. And changed the delimiters for the date constants to single quotes. Using double quotes for strings can lead to confusion.
EDIT:
All this said, the query in the question looks like it is syntactically correct. I notice that the error message does not refer to this exact query. The query has product='test') b and the error message has product='Conversions') as b. Perhaps there are other differences as well.