Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
Last active May 27, 2022 12:32
Show Gist options
  • Save DominikStyp/51320d6d4459e902e6425abae40a1d07 to your computer and use it in GitHub Desktop.
Save DominikStyp/51320d6d4459e902e6425abae40a1d07 to your computer and use it in GitHub Desktop.
SQL & MySQL: Using local variables in complex queries
set @s1 = '%123aaabbbcccdddeeefffggg%';
select * from `items` where
(
`title` LIKE @s1 or `apn` LIKE @s1 or `slug` LIKE @s1
or exists (select * from `sellers` where `items`.`seller_id` = `sellers`.`id`
and (`title` LIKE @s1 or `slug` LIKE @s1 or `address` LIKE @s1)
)
or exists (select * from `item_geos`
where `items`.`id` = `item_geos`.`item_id`
and (`state` LIKE @s1 or `acreage` LIKE @s1 or `county` LIKE @s1 or `city` LIKE @s1
or `address` LIKE @s1 or `zip` LIKE @s1
)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment