H
H
hrvasiliy2016-07-07 01:01:43
MySQL
hrvasiliy, 2016-07-07 01:01:43

How to make a selection on 2 identical fields?

Table example: sqlfiddle
I'm trying to figure out how to make a selection on 2 fields, for example, car_brand and name. The point is that Ivan can have several cars, I'm trying to select cars of a certain person and only one brand, after the selection I write the ID to the database and set status = 1 and continue the selection. That is, in this example, after the first request, I should get: the 1st and 5th lines. (Ivan Ivan and Honda Honda)...
I tried different ways, it doesn't work.
UPD:
Modeling what I'm trying to do.
1. sqlfiddle.com/#!9/0ecfec/80
2. sqlfiddle.com/#!9/dede9/3
3. sqlfiddle.com/#!9/375b08/13
4. sqlfiddle.com/#!9/968202 /1
5.sqlfiddle.com/#!9/24944/1
But I'm not sure if this will always work correctly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2016-07-07
@ThunderCat

UPD2: In short: From long dialogues - the task is to bypass the entire database, select 1 user each, then select all the same user-brand pairs, do shamanism with them, write back status 1.
1) Get possible user-machine pairs, remember in an array .

SELECT distinct
  `name`, `car_brand` 
FROM 
  `drivers`
where `status` = 0

2) then we sort through the values ​​and substitute them into the query:
SELECT *
FROM   `drivers`
where   `name` = '$name' and `car_brand`= '$brand' and `status` = 0

3) we get a sample, we do magic, we do an update by id.

X
xmoonlight, 2016-07-07
@xmoonlight

SELECT *, COUNT(car_brand) as total_cars
FROM 
  `drivers`
GROUP BY car_brand, name

We take the 1st record and substitute the values ​​(all subsequent ones are similar):
SELECT  *
FROM   `drivers`
    where 
    `car_brand` = "Honda"
    and 
    `name` = "Иван"
    and 
    `status` = 0
LIMIT 2

and we get:
id	car_brand	name 	status
------------------------------------------
1 	Honda 		Иван 	0
5 	Honda 		Иван 	0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question