dbTalk Databases Forums  

Embedded table in sql string?

comp.databases.mysql comp.databases.mysql


Discuss Embedded table in sql string? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SpreadTooThin
 
Posts: n/a

Default Embedded table in sql string? - 02-23-2011 , 11:48 AM






I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.

but what if I wanted to do this for another name like Mac or OB or OC

Can I put a list of 'likes' in the SQL statement?

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE (%Mc%,
%Mac%, %OB%, %OC%) ORDER BY LastName

and order them by the Mc, Mac, OB, OC parameter...

What does the syntax look like for this if it can be done?

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 12:20 PM






On 23-02-11 18:48, SpreadTooThin wrote:
Quote:
I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.

but what if I wanted to do this for another name like Mac or OB or OC

Can I put a list of 'likes' in the SQL statement?

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE (%Mc%,
%Mac%, %OB%, %OC%) ORDER BY LastName

and order them by the Mc, Mac, OB, OC parameter...

What does the syntax look like for this if it can be done?

SELECT LastName, Nationality, 1 FROM Clients WHERE LastName LIKE %Mc%
UNION
SELECT LastName, Nationality, 2 FROM Clients WHERE LastName LIKE %Mac%
UNION
SELECT LastName, Nationality, 3 FROM Clients WHERE LastName LIKE %OB%
UNION
SELECT LastName, Nationality, 4 FROM Clients WHERE LastName LIKE %OC%
ORDER BY 3, LastName

--
Luuk

Reply With Quote
  #3  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 01:03 PM



On 2011-02-23 18:48, SpreadTooThin wrote:
Quote:
I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.

but what if I wanted to do this for another name like Mac or OB or OC

Can I put a list of 'likes' in the SQL statement?

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE (%Mc%,
%Mac%, %OB%, %OC%) ORDER BY LastName

and order them by the Mc, Mac, OB, OC parameter...

What does the syntax look like for this if it can be done?

You could use a temporary table, insert your patterns, and join it
against clients. I'll use a derived table here, but the principle is the
same:

select c.*
from ( select 'Mc%' as pat
union
select 'Ab%' as pat) x
join clients c
on c.lastname like x.pat;

If you want a specific order:

select c.*
from ( select 'Mc%' as pat, 1 as nr
union
select 'Ab%' as pat, 2 as nr) x
join clients c
on c.lastname like x.pat
order by nr;


/Lennart

Reply With Quote
  #4  
Old   
SpreadTooThin
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 02:04 PM



On Feb 23, 11:20*am, Luuk <L... (AT) invalid (DOT) lan> wrote:
Quote:
On 23-02-11 18:48, SpreadTooThin wrote:



I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.

but what if I wanted to do this for another name like Mac or OB or OC

Can I put a list of 'likes' in the SQL statement?

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE (%Mc%,
%Mac%, %OB%, %OC%) ORDER BY LastName

and order them by the Mc, Mac, OB, OC parameter...

What does the syntax look like for this if it can be done?

SELECT LastName, Nationality, 1 FROM Clients WHERE LastName LIKE %Mc%
UNION
SELECT LastName, Nationality, 2 FROM Clients WHERE LastName LIKE %Mac%
UNION
SELECT LastName, Nationality, 3 FROM Clients WHERE LastName LIKE %OB%
UNION
SELECT LastName, Nationality, 4 FROM Clients WHERE LastName LIKE %OC%
ORDER BY 3, LastName

--
Luuk
Thanks Luuk.
The 1, 2, 3, 4 that you added to the Select statement are constants
that end up in the record set?
If so, then what is the purpose of the 3 in the 'order by 3,
Lastname'?

Reply With Quote
  #5  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 02:54 PM



On Feb 23, 5:48*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:
Quote:
I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.
as well as ones like "Smack" & "Bob" & "Rock"

Reply With Quote
  #6  
Old   
Luuk
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 03:35 PM



On 23-02-11 21:04, SpreadTooThin wrote:
Quote:
On Feb 23, 11:20 am, Luuk <L... (AT) invalid (DOT) lan> wrote:
On 23-02-11 18:48, SpreadTooThin wrote:



I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.

but what if I wanted to do this for another name like Mac or OB or OC

Can I put a list of 'likes' in the SQL statement?

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE (%Mc%,
%Mac%, %OB%, %OC%) ORDER BY LastName

and order them by the Mc, Mac, OB, OC parameter...

What does the syntax look like for this if it can be done?

SELECT LastName, Nationality, 1 FROM Clients WHERE LastName LIKE %Mc%
UNION
SELECT LastName, Nationality, 2 FROM Clients WHERE LastName LIKE %Mac%
UNION
SELECT LastName, Nationality, 3 FROM Clients WHERE LastName LIKE %OB%
UNION
SELECT LastName, Nationality, 4 FROM Clients WHERE LastName LIKE %OC%
ORDER BY 3, LastName

--
Luuk

Thanks Luuk.
The 1, 2, 3, 4 that you added to the Select statement are constants
that end up in the record set?
If so, then what is the purpose of the 3 in the 'order by 3,
Lastname'?
On http://dev.mysql.com/doc/refman/5.0/en/select.html
it says:
SELECT
......
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]


After ORDER BY its possible to use a col_name, an expression, or a
position. This last one is what is used above..


--
Luuk

Reply With Quote
  #7  
Old   
SpreadTooThin
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 04:35 PM



Quote:
as well as ones like "Smack" & "Bob" & "Rock"
Sometime people take analogies toooo far...

Reply With Quote
  #8  
Old   
Norman Peelman
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 06:39 PM



SpreadTooThin wrote:
Quote:
I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.

but what if I wanted to do this for another name like Mac or OB or OC

Can I put a list of 'likes' in the SQL statement?

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE (%Mc%,
%Mac%, %OB%, %OC%) ORDER BY LastName

and order them by the Mc, Mac, OB, OC parameter...

What does the syntax look like for this if it can be done?

SELECT LastName, Nationality FROM Clients WHERE
LastName LIKE '%Mc%' OR
LastName LIKE '%Mac%' OR
LastName LIKE '%OB%' OR
LastName LIKE '%OC%'
ORDER BY LastName ASC



If your programming/scripting language has support for it, you can store
a soundex of the name and search by that.

--
Norman
Registered Linux user #461062
-Have you been to www.mysql.com yet?-

Reply With Quote
  #9  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-23-2011 , 10:06 PM



On 2/23/2011 7:39 PM, Norman Peelman wrote:
Quote:
SpreadTooThin wrote:
I have an sql statement that needs to be executed of a list of
parameters.

say for example I have a table of Clients [LastName, Nationality]

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE %Mc%

So this gives me a list of all people like McNeil, McKnight and their
nationality.

but what if I wanted to do this for another name like Mac or OB or OC

Can I put a list of 'likes' in the SQL statement?

SELECT LastName, Nationality FROM Clients WHERE LastName LIKE (%Mc%,
%Mac%, %OB%, %OC%) ORDER BY LastName

and order them by the Mc, Mac, OB, OC parameter...

What does the syntax look like for this if it can be done?


SELECT LastName, Nationality FROM Clients WHERE
LastName LIKE '%Mc%' OR
LastName LIKE '%Mac%' OR
LastName LIKE '%OB%' OR
LastName LIKE '%OC%'
ORDER BY LastName ASC



If your programming/scripting language has support for it, you can store
a soundex of the name and search by that.

Which does not provide the sort order the op requests.

Luuk's response, while a bit more complicated, is also correct.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
  #10  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: Embedded table in sql string? - 02-24-2011 , 03:25 AM



On Feb 23, 10:35*pm, SpreadTooThin <bjobrie... (AT) gmail (DOT) com> wrote:
Quote:
as well as ones like "Smack" & "Bob" & "Rock"

Sometime people take analogies toooo far...
I was pointing out that your LIKE arguments would produce names were
those letters appear anywhere in the name, not only where they are at
the beginning as your post seemed to suggest.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.