I'm having a hard time with a query in MySQL.
I'm working with Delphi XE and I'm sending a query with some parameter to MySQL. Here's an example for the query:
SELECT * FROM users u WHERE u.id IN (:idUsers);
The ':idUsers' is the variable that will receive the parameter I send with Delphi, which is a string containing that is formatted like this, ex.: 1,2,3
The problem is that with this string, I receive only the first user (id = 1). From what I can see, its just like MySQL is adding some quote ('') at the beginning and at the end of the string I send, like if it was '1,2,3' instead of 1,2,3. I've tried this select :
SELECT * FROM users u WHERE u.id IN ('1,2,3');
and it does indeed return only the first user..
I had a function in MSSQL that was splitting the string I sended and returning a temporary table, but we recently switched to MySQL, and from what I read, MySQL doesn't allow returning a table.
Anyone has a clue on how to solve this problem? I've been scrapping the Web for an answer and haven't found one for this specific problem.
Thanks for the help.
Parameters don't work like that; they have no idea that what you're trying to provide is a comma-separated list of values. If you set ParamByName('WhatEver').AsString or Value, it thinks you mean a quoted string that contains everything that belongs in that parameter. So it's doing exactly what it appears to - it's passing IN ('1, 2, 3'), instead of the intended IN (1, 2, 3).
You'll need to either go back to parsing it out yourself and using a temp table, or build the WHERE clause dynamically and concatenating it before executing the query. (Actually, you could build the query dynamically, creating a new parameter for each of the items in the IN clause, and then loop through and assign values to each of those dynamically created parameters, but that gets very ugly very quickly.)
If you have a variable number of parameters in the IN clause, you could build a temporary table in MYSQL (which only contains a column called ID), load the parameter values into the temporary table and then do something like this:
SELECT * FROM users u WHERE u.id IN (SELECT ID FROM TEMPTABLE);
As long the TEMPTABLE only contains the values you want to query, the table space scan is acceptable.
You can then have a variable number of values. You could also make the table permanent, and store the parameters. Add a column for the User, and each user can store their individual parameters.
Related
I'm wanting to know if it's possible to pass a variable into an SQL statement for a query, rather than making 12 different queries which would also have me making 12 different forms (1 for each query). The table name to update depends on the column "what_table" in a table named "weekinfo".
currently the SQL statement is:
SELECT wk1_info.ID, wk1_info.Player_Fname, wk1_info.Player_Lname, wk1_info.email, wk1_info.postion
FROM wk1_info
WHERE (((wk1_info.postion) Like 0));
is it possible to declare the table this way, or would I have to make the extra queries and forms? If it is possible could I pointed in the right direction?
No. Table and field names cannot be passed as parameters.
What you can do is to write the full SQL from a template string replacing tokens for table and/or fields names with those you need. Then run/execute the finished SQL string.
I am using a SSIS Data Flow Task to transfer data from one table to another. Column A in Table A contains a number, the last 3 digits of which I want to store in Column B of Table B.
First I'm trying to grab all of the data in Column A and store in a variable via a simple SELECT statement SELECT COLUMN_A FROM TABLE_A. However, the variable stores the statement as a string when I want the result set of the query. I have set the EvaluateAsExpression property to False but to no avail.
Secondly I want to be able to use the result of this query in the Derived Column of my Data Flow to extract the last 3 digits and store the values in Column_B in the destination. The expression I have is:
(DT_STR,3,1252)RIGHT(#User::[VariableName],3)
I want to store this as a string hence the (DT_STR,3,1252) data type.
All I'm getting so far in Column_B of Table_B is is the last 3 characters of the SELECT statement "E_A". There is a lot of useful information on the web including YouTube videos for things like setting file paths and server names as parameters or variables but I can't see many relevant to the specifics of my query.
I have used an Execute SQL Task to insert row counts from flat files but, in this example, I want to use the Derived Column tool instead.
What am i doing wrong? Any help is gratefully appreciated.
I prefer to do all the work in SQL if you aren't doing anything else with that number.
select right(cast(ColA as varchar(20)),3) from tableA
-- you can add another cast if you want it to be an int
use that in an execute sql to result set = single row.
Map that to a variable.
In a derived column in data flow you can set that variable to the new column.
Thanks KeithL thats one solution I will use in future but I found another.
I dropped the variable and in the Expression box of the Transformation Editor did:
(DT_STR,3,1252)RIGHT((DT_STR,3,1252)Column_A,3).
In my question, I failed to cast Column_A from Table_A as a string. The first use of (DT_STR,3,1252) simply sets the destination column as a string so as not to use the same data type as the source which in my case was int.
Its the 2nd use of (DT_STR,3,1252) that actually casts Column_A from int to a string.
I am trying to Update a column in my table Inputcounts called concatenate off of a query called InputConcatenates that has a column also called concatenate. I am running an update query with the field name as concatenate the table name as InputCounts and the update to field as [InputConcatenates].[Concatenate]. But every time I run the query it pulls back that 0 records will be updated. Is my syntax wrong possibly?
Update Query SQL:
UPDATE InputCounts INNER JOIN InputConcatenate
ON InputCounts.CONCATENATE = InputConcatenate.CONCATENATE
SET InputCounts.CONCATENATE = [InputConcatenate].[CONCATENATE];
InputConcatenate Query SQL:
SELECT InputCounts.FLEET, InputCounts.AMMs, [FLEET] & [AMMs] AS CONCATENATE
FROM InputCounts;
You reported this query accomplishes what you want ...
UPDATE InputCounts
SET CONCATENATE = [FLEET] & [AMMs]
WHERE CONCATENATE Is Null;
That may be fine. However CONCATENATE is not updated until you execute the UPDATE, and does not get updated (after having previously received a value) in response to changes in FLEET or AMMs
Decide whether CONCATENATE really needs to exist as a field in your table. You could use a query to derive it whenever you need it:
SELECT *, FLEET] & [AMMs] AS CONCATENATE
FROM InputCounts;
With the query, CONCATENATE will always be up to date.
If your database is ACCDB format and your Access version is >= 2010, another possibility is to make CONCATENATE a "calculated field" type in the table's design:
If you prefer CONCATENATE be Null whenever FLEET or AMMs is Null, change the field's Expression property to [FLEET] + [AMMs]
The advantage of a calculated field is that Access automagically updates its value without further effort (like executing an UPDATE) from you.
A disadvantage is that you can't index a calculated field. That means it's not suited for joins, WHERE criteria, ORDER BY, etc. You'll have to decide whether it's a reasonable fit for your application. :-)
I'm working on dynamic pages. The scenario goes like this.
Page 1 consists of a drop down menu for me to select.
The selected menu value will pass to the next page URL.
Then this value will be retrieved by the page to compare and filter the MYSQL Table field name.
The table consists of 4 main categories which was the option given to the user to select in page 1.
For each category there will be 30 words stored.
When the user searches for a certain word, it is supposed to ask for an option by displaying the drop down and checking if this value is the same as the value of field id of the table and the word is now used to narrow down to just search in this field instead of all 4 categories.
How to do this? I was working on this for few weeks but I can't solve it.
$ret1 = "SELECT * FROM $db_tb_name WHERE MATCH
('categoryid'='.$selectvalueid.') AND AGAINST ('%$str%' IN BOOLEAN MODE)";
and I have this piece of code and it has wrong syntax.
Please someone help me with some example.
So let me get this straight: at the time you issue your request, you have both a category id $selectvalueid (resulting from the drop down) and a search term $str (resulting from a free text entry?). And you want those rows where the categoryid column of your table exactly matches the $selectvalueid string from your script, while some other column (with what name?) contains the $str value. Is that correct so far?
If it is, your query might look like this:
SELECT * FROM $db_tb_name
WHERE categoryid = '$selectvalueid'
AND wordcolumn LIKE '%$str%'
Note that simply pasting user input variables into SQL queries most likely will make your site vulnerable to SQL injection attacks. So a better way would be using queries with placeholders, and using prepared statements to fill in those placeholders in a safe way when executing the query. How to do that depends on the application language.
I want to be able to search a field in the database and see if the "changed from" and "to" values are different(the values are stored within a string in a single field).
Here is an example of what the string in the field looks like
Instance Person(34) modified by 1.
Field phone_number changed from "123" to "123".
Current field values are:
first_name => "alex"
last_name => "Handley"
In this example they are the same so would not be returned.
is it possible to do this ?
Alex
This is really a job for a regular expression, not a database query.
You could do it with a stored procedure which makes a query, chops up the result, and compares the two parts. But, again, this type of logic belongs at the application layer - either beforehand, storing the parts of the string you'll need to compare separately, or afterwards, extracting the useful info from the single database column.
MySQL has pretty nice regular expression support. (It's also fast; I've found that REGEXP is often faster than LIKE).
I don't believe that it has back reference support, so it would be difficult to compare in a single query. You might be able to do it with a clever join or a stored procedure.