J
J
Jack Cooper2016-02-13 16:33:57
MySQL
Jack Cooper, 2016-02-13 16:33:57

How to compose a MySQL query?

Good day everyone! I'm new to MySQL and have a question.
Suppose I have a base with vegetables (vegetables), they go through supply cities.
For example:
Potato goes this way - Minsk -> Moscow -> Perm -> Tyumen.
I'm looking for what vegetables pass the way Moscow and Tyumen , Potatoes should be shown to me .
The database has: the beginning of the path - Moscow and the end of the path - Tyumen, the segment - Moscow and Perm are intermediate points (they may not be).
There is also a back (field name) enum value of 1 or 0, 1 - means that the vegetable can be sent back, 0 - cannot.
That is, if I look for Perm -> Moscow, then the potatoes will still seem, as if in the opposite direction. If it would be back = 0 then no.
There may be similar combinations in which potatoes will be found:

Start - Moscow, end - Perm,
Start - Minsk, end - Tyumen,
Start - Tyumen, end - Minsk (if back == 1)

How to build a database search query?
Fields:
start - start of path type varchar(64)
end - end of path varchar(64)
gap - intermediate points in path, type: blob stored array serilize


Many thanks to those who write at least part of the request, or provide some example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Marat, 2016-02-13
@negmat

Few inputs in the SQL storage part.
If, for example, you organize your structure as a list of tables (id - primary keys):
vegetables(id, name) vegetables. Entries in the table:(1, 'potato')
points(id, name) transportation points. Entries in the table: (1, 'Minsk'), (2, 'Moscow'), (3, 'Perm'), (4, 'Tyumen')
deliveries(vegetable, route, name). Entries in the table: (1, 1, 'Supply of potatoes from Belarus N 120'). The fields have foreign keys on vegetables and routes
routes (id, StartPoint, StopPoint). Entries in the table: (1, 1, 4). The fields have foreign keys on points
pathes(route,fromPoint, toPoint). Entries in the table: (1,1,2) , (1,2,3) (1,3,4). The fields have foreign keys on points and routes.

select  distinct vegetables.name 
  from vegetables, deliveries, routes, pathes
  where 
    deliveries.vegetable = vegetables.id and
    routes.id=deliveries.route and
    pathes.route=deliveries.route and
   exists (
      select * from pathes, points 
         where pathes.route=route.id and 
            from pathes.fromPoint=points.id and points.Name='Москва') and
   exists (
      select * from pathes, points 
         where pathes.route=route.id and 
            from pathes.toPoint=points.id and points.Name='Тюмень')

will give vegetables that have a supply that runs through 2 specified cities.
PS Naturally, additional fields are added (the presence of a reverse route, etc.). I just wanted to show (as an example) - the organization of data sampling and their storage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question