I'm trying to create a mailmerge that will insert a SQL query into the document. That query will need to reference an Employee ID number. The mergefields are First_Name, Last_Name, and Emp_ID.
The gist is that the merge will be personalized with the name, and then some boilerplate language, and then list all the accounts the person has dealt with.
Here's where I am with the database field code:
DATABASE \d "M:\\Acme\\Testing Sandbox\\Database11.accdb" \s "SELECT DISTINCT SalesDB.[Parent Acct Name] FROM SalesDB WHERE SalespersonNumber = 9999"
That's functional and great for Employee 9999, but I've got a few hundred of these to create. How can I make SalespersonNumber reference the Emp_ID field I brought in using mail merge? Something like below, but, you know, functional.
DATABASE \d "M:\\Acme\\Testing Sandbox\\Database11.accdb" \s "SELECT DISTINCT SalesDB.[Parent Acct Name] FROM SalesDB WHERE SalespersonNumber = {Emp_ID}"
If the database column's data type is numeric, you need this:
DATABASE \d "M:\\Acme\\Testing Sandbox\\Database11.accdb" \s "SELECT DISTINCT SalesDB.[Parent Acct Name] FROM SalesDB WHERE SalespersonNumber = {MERGEFIELD Emp_ID}"
If the database column's data type is actually a string type, you need to surround the MERGEFIELD with quotation marks like this:
DATABASE \d "M:\\Acme\\Testing Sandbox\\Database11.accdb" \s "SELECT DISTINCT SalesDB.[Parent Acct Name] FROM SalesDB WHERE SalespersonNumber = '{MERGEFIELD Emp_ID}'"
Either way, the {MERGEFIELD Emp_ID} code has to be a proper field code, I.e. the {} have to be the field code braces that you can insert using ctrl-F9, not the regular keyboard characters. If you are inserting the DATABASE field using code (e.g. VBA) then you will need to pay attention to that.
Related
I'm noticing when I make any select statement with json data outputs a column name with random title e.g.
select 'john' as firstname, 'smith' lastname for json path
if I run this in sql management studio (text results) I'll get
JSON_F52E2B61-18A1-11d1-B105-00805F49916B
-------------------------------------------- [{"firstname":"john","lastname":"smith"}]
(1 row(s) affected)
How to change the column name of the generated json data. I've tried using the root option but couldn't override the column title.
This is the same as using XML.
you cannot set the column name
for my opinion since you will always get single row and single column (which means that this is only one value)
the column name have no meaning. but maybe you have different scenario that i am not aware of.
anyway, if you want to workaround it you can use this query
select (select 'john' as firstname, 'smith' lastname for json path) as MyColumn
I am trying to get data from my database. I have no problems with this, the SELECT statement works fine, but I want to select the record with a symbor or text before the result.
When I run the code:
SELECT price
FROM products
WHERE id = "1"
Ill get 5.00. But in this case I want to select it with a "€" symbol before the price. I found some scripts on this site where they are using the following code:
SELECT "€ " + price as 'price'
FROM products
WHERE id="1"
When I run this i get the same result as in the first code. So my question is: How can I select the price from the database with a symbol or text before (or after) the result?
Since you specified what is your RDBMs your answer is:
SELECT CONCAT('€ ', price) as price
FROM products
WHERE id=1
Don't use double quotes to use strings, double quotes on SQL is to name things like columns as alias and only use it when you want a field named with characters that the database wouldn't allow it like "Column name with spaces"
Also, ID is probably a number, so no need to use quotes since it will imply in implicit conversion which will make your query slower.
I Have a string foo = "a,b". Now I want to search in the mysql database to get user_id while comparing the string to the likes field. The likes field has data in the format interest => a c v d b.
The different characters are seperated by a space. I tried Using like but the result was not upto the mark. How can I go about it?
This is my code
select user_id from users where interest like %foo%;
MySql does not support multiple keyword search in set like field, you should add OR condition of each search keyword with REGEXP
if your format interest like=> a,c,v,d,b then you can use FIND_IN_SET() function otherwise REGEXP provide to exact search.
SELECT user_id FROM users
WHERE interest REGEXP '[[:<:]]a[[:>:]]' AND interest REGEXP '[[:<:]]b[[:>:]]'
this query search only a and b in field not aa, bbax
LIKE does not support exact search.
There is table named Students. I want to extract the names of students whose name starts with either 'n' or 'p' or 'y'. I know that in TSQL (MS SQL server) I can write the query as follows and it works:
SELECT * FROM Students WHERE StudentName LIKE '[npy]%'
But when I execute the same query in MySQL (phpmyadmin) I was unable to retrieve the correct result set. I tried converting the letters of the student name into the same case which is mentioned in the charlist. I did bit of googling and found out that in MySQL we need to specify this as a regular expression. I executed the below query and got the expected result.
SELECT * FROM Students WHERE StudentName REGEXP '[[:<:]]n | [[:<:]]p | [[:<:]]y'
I want to know the reason why LIKE '[charlist]%' syntax is not working with MySQL. Is it due to the implementation in MySQL (MySQL doesn't support the syntax) or something wrong with the phpmyadmin version I'm using?
Any help regarding this is highly appreciated. Thanks :)
There is an even shorter way to write your query:
SELECT * FROM Students WHERE StudentName REGEXP '^[npy]'
And if you're concerned about being case sensitive:
SELECT * FROM Students WHERE StudentName REGEXP BINARY '^[npy]'
In MySQL, a REGEXP pattern match succeeds anywhere in the value, which differs from LIKE where the pattern must match the entire value.
The following link will give you a more complete answer:
MySQL Pattern Matching
MySQL :
Case insensitive: SELECT * FROM Students WHERE StudentName RLIKE '^[npy]' ;
Case sensitive : SELECT * FROM Students WHERE StudentName CAST(RLIKE as BINARY) '^[npy]' ;
I am not able to run a simple select query with a where clause.
I am using MySQL and have a column User ID.
The problem is with the column name made of two words.
select * from user where 'User ID' = "xyz"
A usual query like the next one runs fine as expected:
select * from user where email = 'xyz'
How can I write a condition on the User ID column?
Try:
SELECT u.*
FROM `user` u
WHERE u.`User ID` = 'xyz';
But in general, try not to use such column names.
Using backticks to qualify the table and/or column names is also useful if you have names that conflict with MySQL keywords, e.g. user.
No way to rename this column ?
You can try with backticks around the column name in your query :
select * from user where `User ID` = 'xyz';
Like Jonathan Leffler pointed, if you are in MS SQL Server you can try:
select * from user where [User ID] = "xyz"
or in MySql PHP Admin:
select * from user where ´User ID´ = "xyz"
I am not sure about MySql, but in SQL Server, even "login" or "user" that are some reserved words, they are functional in queries normally. Despite, I think is better not to use that.
I hope this can help you or another one. Hugs.
Ref.:
1. Meaning of square brackets [] in MS-SQL table designer?
2. When to use single quotes, double quotes, and backticks in MySQL
I think user is a reserved word...
Try:
SELECT *
FROM `user`
WHERE `email` = 'xyz';