How to loop through each row in MySQL table and get rows if conditions match

1.1k Views Asked by At

For my application I would like to do the following:

SELECT *
FROM `LLS_USERS`
LIMIT 0,111
WHERE 
(`USR_LOGIN`=
    (CONCAT(
            (SELECT LEFT(`USR_FIRST_NAME`, 1) FROM `LLS_USERS`;),
            (SELECT `USR_LAST_NAME`FROM `LLS_USERS`;)
           );
    )
)

Basically I need to select all the rows in the user table where the user's login matches up with the first initial of the user name concatinated with the user's last name. So the SQL query will generate a table for me of all the selected rows where this is true in phpMyAdmin. I know I have 111 users in my database currently.

Does anyone know what's wrong with my syntax?

UPDATE: SOLUTION is:

SELECT *
FROM `LLS_USERS`
WHERE (
`USR_LOGIN` = ( left( `USR_FIRST_NAME` , 1 ) || `USR_LAST_NAME` )
)
LIMIT 0 , 111; 
1

There are 1 best solutions below

0
On

Take SQL as a (real life) Language here in this basic example:

Give me all rows of a table that match the condition loginname= 1 char of first name following lastname

->

SELECT * FROM LLS_USERS where USR_LOGIN= LEFT(USR_FIRST_NAME, 1)||USR_LAST_NAME

Thats all, thats the beautiness of SQL