I have a problem when I want to fetch data from data base
The problem lies in the use of(-) exemple (code-produit)
With that I can not change anything in data base
Requet:
SELECT `Commande.numéro-Commande`,`Date`,`Code-Produit`,`Désignation`,`Prix-Unitaire`,`Qte`
FROM `client`,`commande`,`produit`,`ligne-commande`
WHERE `client.Code-client`=`commande.Code-client` and
`commande.Numèro-commande`=`ligne-commande.Numèro-commande` and
`ligne-commande.Code_Produit`=`produit.Code-Produit` and
`code-client`=5
Error:
Set the table for the last code-client (code-client=5), it could be from client or commande, so set the table
If you want to use hyphen - in your table/column name(s), you should consider wrapping it using backticks. Otherwise, MySQL parser would read it as subtraction operator. My recommendation is to use underscore _ instead of hyphen -.
Secondly, please don't use Old comma based Implicit joins and use Modern Explicit Join based syntax
SELECT Commande.`numéro-Commande`,
Date,
`Code-Produit`,
Désignation,
`Prix-Unitaire`,
Qte
FROM client
JOIN commande ON client.`Code-client` = commande.`Code-client`
JOIN produit ON `ligne-commande`.Code_Produit=produit.`Code-Produit`
JOIN `ligne-commande` ON commande.`Numèro-commande` = `ligne-commande`.`Numèro-commande`
WHERE client.`code-client` = 5
Related
I am needing to use concat to create custom sql statements.
I'm trying to use
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1), 'nam.jpg')
The goal is to update the image_loc in setuptable to "/file/to/image/UIDnam.jpg"
I'm getting Syntax errors and "Expected End of Statement".
How can i tie a simple string, a subquery, and another simple string together to update a field?
I found a way to make it work. If any one has any tips of improvement, I'm up to learn!
*I can't use variables
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1)+, 'nam.jpg')
I found slightly better following structure :
update setuptable
set image_loc = CONCAT('/file/to/image/', t1.UID, 'nam.jpg')
from table1 t1
I don't have mysql server at hand for the moment, so I have not tested.
Here is my query for a maintenance dates list.
SELECT `checkdates`.`checkdateplanneddate`, `checkdates`.`checkdatevehicle`, `checktypes`.`checktype`, `checktypes`.`emailto`, `checktypes`.`daysnotice`
FROM `checkdates`
, `checktypes`
WHERE `checktypes`.`checktype` = `checkdates`.`checkdatechecktype`;
The idea is..
Everyday the server will email customers to let them know which checkdates are coming, based on the days notice that is set for that type of check. (see image)
Currently it is showing all checkdates.
All i need to do is filter the list so it only shows the dates that are
"Todays date plus checktypes.daysnotice"
I have tried many different queries, but cannot seem to get the right combo.
Thank you in advance
I have attached an image to show that the data is available
If I understand your question correctly, and assuming that you are running MySQL (as the use of backticks for quoting and the phpmyadmin screen copy indicate), you can use date arithmetics as follows:
SELECT cd.checkdateplanneddate, cd.checkdatevehicle, ct.checktype, ct.emailto, ct.daysnotice
FROM checkdates cd
INNER JOIN checktypes ct ON ct.checktype = cd.checkdatechecktype
WHERE cd.checkdateplanneddate = current_date + interval ct.daysnotice day
The where condition implements the desired logic.
Side notes:
Use standard, explicit joins! Implicit joins (with commas in the from clause) is a very old syntax, that should not be used in new code
Table aliases make the query easier to write and read
I'm having trouble generating a query in Codeigniter. The problem is
$this->db->select('user.*')
->join('user_group', 'user.group_id BETWEEN user_group.start_range AND user_group.end_range', 'left');
This code generates the following query:
SELECT `user`.* FROM (`user`) LEFT JOIN `user_group` ON `user`.`group_id` `BETWEEN` user_group.start_range AND user_group.end_range
Here, the mysql can not recognize the BETWEEN which is inside the quote character, how can I generate the query without wrapping with the quote character. Please, give me any suggestion.
I'm using codeigniter 2.2.0
You would need to either override the $_reserved_identifiers variable in the CI_DB_driver class to look like the following
var $_reserved_identifiers = array('*', 'BETWEEN'); // Identifiers that should NOT be escaped
(Note im not sure what if this reduces security)
https://github.com/bcit-ci/CodeIgniter/blob/2.2-stable/system/database/DB_driver.php#L67
The other option is to replicate the between operator using >= and <=
Just a quick question about naming columns that come from other tables, below i have the tables put in the SQL statement but after it I put an abbreviated version "MO" is this correct/ will this work in all situations or should i just stick to the full version like module.mod_code?
SELECT MO.MOD_CODE, MO.MOD_NAME, MO.ECTS_UNITS,MO.DESCRIPTION
FROM MODULE MO, SYLLABUS SY, PROGRAMME PR
WHERE MO.MOD_CODE = SY.MOD_CODE
AND SY.PROG_CODE = PR.PRO_CODE
AND PR.NFQ_LEVEL = ‘LEVEL 9’
AND MO.DESCRIPTION LIKE ‘%RESEARCH%’ OR DESCRIPTION LIKE ‘%QUALATIVE%’ OR DESCRIPTION LIKE ‘%QUANTITATIVE%’;
Thanks :)
If I understood correctly, you're trying to reference columns using the table alias, and are wondering if there is any difference in using MO.[column] and module.[column]?
If that is the case, it is preferred to use the table alias to reference the column. This is because you may join back to the same table to retrieve a different subset of data. If you do this, you will need to define which set you want the data to come from.
Module AS M ---- Programme AS P ------ Module AS SUBM
You cannot stick to the full version. Once you have given a table or subquery an alias, that is the name of that object in the scope of the query. Actually, what happens is that the table name becomes the table alias, so you can use it for qualifying columns in the table.
You should also learn proper explicit JOIN syntax. I am also guessing that you are missing parentheses on your WHERE clause:
SELECT MO.MOD_CODE, MO.MOD_NAME, MO.ECTS_UNITS,MO.DESCRIPTION
FROM MODULE MO JOIN
SYLLABUS SY
ON MO.MOD_CODE = SY.MOD_CODE JOIN
PROGRAMME PR
ON SY.PROG_CODE = PR.PRO_CODE
WHERE PR.NFQ_LEVEL = 'LEVEL 9' AND
(MO.DESCRIPTION LIKE '%RESEARCH%' OR
MO.DESCRIPTION LIKE '%QUALATIVE%' OR
MO.DESCRIPTION LIKE '%QUANTITATIVE%'
);
If you attempted something like SELECT MODULE.MOD_CODE in this query, it would return an error, because the table alias MODULE is not assigned to any object.
The following doesn't work, but something like this is what I'm looking for.
select *
from Products
where Description like (#SearchedDescription + %)
SSRS uses the # operator in-front of a parameter to simulate an 'in', and I'm not finding a way to match up a string to a list of strings.
There are a few options on how to use a LIKE operator with a parameter.
OPTION 1
If you add the % to the parameter value, then you can customize how the LIKE filter will be processed. For instance, your query could be:
SELECT name
FROM master.dbo.sysobjects
WHERE name LIKE #ReportParameter1
For the data set to use the LIKE statement properly, then you could use a parameter value like sysa%. When I tested a sample report in SSRS 2008 using this code, I returned the following four tables:
sysallocunits
sysaudacts
sysasymkeys
sysaltfiles
OPTION 2
Another way to do this that doesn't require the user to add any '%' symbol is to generate a variable that has the code and exceute the variable.
DECLARE #DynamicSQL NVARCHAR(MAX)
SET #DynamicSQL =
'SELECT name, id, xtype
FROM dbo.sysobjects
WHERE name LIKE ''' + #ReportParameter1 + '%''
'
EXEC (#DynamicSQL)
This will give you finer controller over how the LIKE statement will be used. If you don't want users to inject any additional operators, then you can always add code to strip out non alpha-numeric characters before merging it into the final query.
OPTION 3
You can create a stored procedure that controls this functionality. I generally prefer to use stored procedures as data sources for SSRS and never allow dynamically generated SQL, but that's just a preference of mine. This helps with discoverability when performing dependency analysis checks and also allows you to ensure optimal query performance.
OPTION 4
Create a .NET code assembly that helps dynamically generate the SQL code. I think this is overkill and a poor choice at best, but it could work conceivably.
Have you tried to do:
select * from Products where Description like (#SearchedDescription + '%')
(Putting single quotes around the % sign?)
Dano, which version of SSRS are you using? If it's RS2000, the multi-parameter list is
not officially supported, but there is a workaround....
put like this:
select *
from tsStudent
where studentName like #SName+'%'
I know this is super old, but this came up in my search to solve the same problem, and I wound up using a solution not described here. I'm adding a new potential solution to help whomever else might follow.
As written, this solution only works in SQL Server 2016 and later, but can be adapted for older versions by writing a custom string_split UDF, and by using a subquery instead of a CTE.
First, map your #SearchedDescription into your Dataset as a single string using JOIN:
=JOIN(#SearchedDedscription, ",")
Then use STRING_SPLIT to map your "A,B,C,D" kind of string into a tabular structure.
;with
SearchTerms as (
select distinct
Value
from
string_split(#SearchedDescription, ',')
)
select distinct
*
from
Products
inner join SearchTerms on
Products.Description like SearchTerms.Value + '%'
If someone adds the same search term multiple times, this would duplicate rows in the result set. Similarly, a single product could match multiple search terms. I've added distinct to both the SearchTerms CTE and the main query to try to suppress this inappropriate row duplication.
If your query is more complex (including results from other joins) then this could become an increasingly big problem. Just be aware of it, it's the main drawback of this method.