I would like to write a query in Access 2013 to mark the first time a word appears as "Yes" and all subsequent times as "No". I have included a sample below. The "Distinct" column is what I would like my query to generate.
Thank you
Word Distinct
First Yes
Second Yes
First No
Third Yes
Here is one approach:
SELECT *, IIf(DMin("ID","Table1","[Word]='" & [Word] & "'")=[ID],"Yes","No") AS FirstWord
FROM Table1;
Another:
SELECT *, IIf([ID] = (SELECT TOP 1 ID FROM Table1 AS Dupe WHERE Dupe.Word = Table1.Word ORDER BY Dupe.Word, Dupe.ID), "Yes", "No") AS FirstWord FROM Table1;
DISTINCT is a reserved word so I am avoiding using it as a field name.
Related
I want SQL to show / order the results for the column name first then show results for the description column last.
Current SQL query:
SELECT * FROM products WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
I tried adding order by name, description [ASC|DESC] on the end but that didn't work.
It's for optimizing the search results. If a certain word is found in description it should go last if a certain word is also found in the name column.
You can use a CASE statement in an ORDER BY to prioritize name. In the example below all results where name is matched will come first because the CASE statement will evaluate to 1 whereas all other results will evaluate to 2.
I'm not sure by your problem description what exactly you want the behavior to be, but you can certainly use this technique to create more refined cases to prioritize your results.
SELECT *
FROM products
WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
ORDER BY CASE WHEN name LIKE '%$search_query%' THEN 1 ELSE 2 END
If you want the names first, the simplest order by is:
order by (name like '%$search_query%') desc
MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.
While this is undocumented, when results sets combined by a UNION ALL and not sorted afterwards, they stay in the order returned, as UNION ALL just adds new results to the bottom of the result set. This should work for you:
SELECT * FROM products
WHERE name LIKE '%$search_query%'
UNION ALL
SELECT * FROM products
WHERE (description LIKE '%$search_query%' AND name NOT LIKE '%$search_query%')
I'm trying to query the first letter of a last name in MySQL. I want to skip the first name and query the a certain letter in the last name. Thanks
In SKU_data, which SKU has a buyer whose last name begins with 'M'?
*/
select *
from sku_data
where buyer ;
In your where clause, search on WHERE buyer.LastName LIKE 'M%'. This will return all results that start with M.
You need to use a combination of the substring method and the substring_index method in your sql query.
Your select query should look something like this
SELECT SUBSTRING(last_name,1,1)
I'm assuming that your name field has both first and last name in it, instead of separate fields for first and last name. Use the substring_index method to figure out what last_name should be:
SUBSTRING_INDEX(full_name,' ',-1)
Combining this SQL, we have:
SELECT SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
Now you can use the first_letter field in a where clause to get your desired result:
SELECT *, SUBSTRING(SUBSTRING_INDEX(buyer,' ',-1),1,1) as first_letter
from sku_data
where first_letter = 'M' ;
Assuming that you have a field like say "full_name" which has first name and last name in the same column.
Lets say the full_name in table employee has a value "JEFF MOSTI"
You can simply use the following statement to get what you need
select * from employee where full_name regexp ' M';
I am assuming you are looking for the last name to start with 'M' and that there is a space (' ') between the first and last name.
I did some searching and from one question already posted on stackexchange, the answer was that it was not possible, but I figured to ask. I did not know if it was possible to form a SELECT query to dynamically select which columns will be displayed in a mysql SELECT statement result. Example:
Say I have column names Person, ID, Phone Number, Alt Number for this table:
John | 79 | 800-499-0000 | 800-499-5555
I would like to form a SELECT statement so that it will only pull down columns where string '800-499' is somewhere in the field. Thus the result from MySQL ideally would be:
800-499-0000 | 800-499-5555
The only problem is that I do not think dynamically selecting columns is possible.
Any help or confirmation is appreciated.
You could try something like:
select * from
(select concat(case when col1 like '%800-499-0000%' then concat('col1: ',col1,';') end,
case when col2 like '%800-499-0000%' then concat('col2: ',col1,';') end,
...
case when coln like '%800-499-0000%' then concat('coln: ',coln,';') end)
as search_results
from my_table) sq
where search_results is not null
I'm very new to access. I have a data in my column that looks similar to this:
JONES/KEN
SMITH/TAMMY
MILLER FRED
PICARD.JOHN
Am I able to grab the letters before the first non-alphanumeric?
So my result would be:
JONES
SMITH
MILER
PICARD
How about a derived table:
SELECT Left([Surname],InStr([Surname],[NonAlpha])-1) AS LeftName,
MainTable.Surname
FROM MainTable,
(SELECT " " As NonAlpha From Table1
UNION
SELECT "." As NonAlpha From Table1
UNION
SELECT "," As NonAlpha From Table1
UNION
SELECT "/" As NonAlpha From Table1) AS n
WHERE (((MainTable.Surname) Like "*" & [nonalpha] & "*"));
Table1 is a scratch table, it does contain records but the query will only
return the four assigned rows (,./ )
Maintable is the table with a field Surname, which is the field to be split.
Unfortunately, I don't know of a "Word" function that's available in some languages. I would do it with brute force checking using Instr and then Mid to extract the code. The construct would be very convoluted to get every kind of character.
I've used the iif function and nested it - this the basic format here:
iif (instr (fieldname,"{the character}") > 0,
mid(fieldname,1, instr(fieldname,"{the character}")-1,
fieldname{or go further into ifs})
Using your sample data with Client Name as the field and 3 conditions - space, / and period. IT does work, but it's ugly - you will have to scroll pretty far to the right to get everything:
ShortName: IIf(InStr(1,[client_name]," ")>0,
mid(client_name,1,InStr(1,[client_name]," ")-1),
IIf(InStr(1,[client_name],"/")>0,
mid(client_name,1,InStr(1,[client_name],"/")-1),
IIf(InStr(1,[client_name],".")>0,
mid(client_name,1,InStr(1,client_name],".")-1),
Client_Name)))
Put this in a query based on your table.
Say I have a table similar to:
ID Name
1 Test
2 Contest
3 Fittest
4 Testament
Is there a MySQL query I could use with ordering to allow it to display a specific word first?
For example, users are searching for the word "Test". I have a statement similar to "SELECT * FROM table WHERE NAME LIKE '%Test%'". Could I display results to show things that START with Test begin first followed by everything else, while everything is still in alphabetical order.
So output would be:
Test
Testament
Contested
Fittest
Thanks.
This will put your words that begin with Test at the top, and sort those words plus the remainder of the list in alphabetical order..
SELECT * FROM mytable
ORDER BY CASE WHEN name LIKE 'test%' THEN 0 ELSE 1 END ASC, name ASC
SELECT * FROM table
WHERE NAME LIKE '%Test%'
order by case when name like 'test%' then 0 else 1 end