slice scala LIst by elements value - html

there is List like this
List(List(9, 1, 9),
List(9, 1, 8),
List(8, 2, 7),
List(8, 2, 6),
List(7, 3, 5),
List(7, 3, 4))
i want to slice list by second element, like this
List(List(List(9, 1, 9), List(9, 1, 8)), //second elem is "1"
List(List(8, 2, 7), List(8,2,6)), //second elem is "2"
List(List(7, 3, 5), List(7,3,4)), //second elem is "3"
I can do it by using verbose for loop, but it's too verbose.
I also try to use span, but I fail to get second element.
what is the best way?

val l = List(List(9, 1, 9),
List(9, 1, 8),
List(8, 2, 7),
List(8, 2, 6),
List(7, 3, 5),
List(7, 3, 4))
l.groupBy( {case first::second::tail => second} ).values.toList

Related

AWK: post-processing of the data based on two columns

I am dealing with the post-procession of CSV logs arranged in the multi-column format in the following order: the first column corresponds to the line number (ID), the second one contains its population (POP, the number of the samples fell into this ID) and the third column (dG) represent some inherent value of this ID (which is always negative):
ID, POP, dG
1, 7, -9.6000
2, 3, -8.7700
3, 6, -8.6200
4, 4, -8.2700
5, 6, -8.0800
6, 10, -8.0100
7, 9, -7.9700
8, 8, -7.8400
9, 16, -7.8100
10, 2, -7.7000
11, 1, -7.5600
12, 2, -7.5200
13, 9, -7.5100
14, 1, -7.5000
15, 2, -7.4200
16, 1, -7.3300
17, 1, -7.1700
18, 4, -7.1300
19, 3, -6.9200
20, 1, -6.9200
21, 2, -6.9100
22, 2, -6.8500
23, 10, -6.6900
24, 2, -6.6800
25, 1, -6.6600
26, 20, -6.6500
27, 1, -6.6500
28, 5, -6.5700
29, 3, -6.5500
30, 2, -6.4600
31, 2, -6.4500
32, 1, -6.3000
33, 7, -6.2900
34, 1, -6.2100
35, 1, -6.2000
36, 3, -6.1800
37, 1, -6.1700
38, 4, -6.1300
39, 1, -6.1000
40, 2, -6.0600
41, 3, -6.0600
42, 8, -6.0200
43, 2, -6.0100
44, 1, -6.0100
45, 1, -5.9800
46, 2, -5.9700
47, 1, -5.9300
48, 6, -5.8800
49, 4, -5.8300
50, 4, -5.8000
51, 2, -5.7800
52, 3, -5.7200
53, 1, -5.6600
54, 1, -5.6500
55, 4, -5.6400
56, 2, -5.6300
57, 1, -5.5700
58, 1, -5.5600
59, 1, -5.5200
60, 1, -5.5000
61, 3, -5.4200
62, 4, -5.3600
63, 1, -5.3100
64, 5, -5.2500
65, 5, -5.1600
66, 1, -5.1100
67, 1, -5.0300
68, 1, -4.9700
69, 1, -4.7700
70, 2, -4.6600
In order to reduce the number of the lines I filtered this CSV with the aim to search for the line with the highest number in the second column (POP), using the following AWK expression:
# search CSV for the line with the highest POP and save all lines before it, while keeping minimal number of the lines (3) in the case if this line is found at the beginning of CSV.
awk -v min_lines=3 -F ", " 'a < $2 {for(idx=0; idx < i; idx++) {print arr[idx]} print $0; a=int($2); i=0; printed=NR} a > $2 && NR > 1 {arr[i]=$0; i++}END{if(printed <= min_lines) {for(idx = 0; idx <= min_lines - printed; idx++){print arr[idx]}}}' input.csv > output.csv
thus obtaining the following reduced output CSV, which still has many lines since the search string (with highest POP) is located on 26th line:
ID, POP, dG
1, 7, -9.6000
2, 3, -8.7700
3, 6, -8.6200
4, 4, -8.2700
5, 6, -8.0800
6, 10, -8.0100
7, 9, -7.9700
8, 8, -7.8400
9, 16, -7.8100
10, 2, -7.7000
11, 1, -7.5600
12, 2, -7.5200
13, 9, -7.5100
14, 1, -7.5000
15, 2, -7.4200
16, 1, -7.3300
17, 1, -7.1700
18, 4, -7.1300
19, 3, -6.9200
20, 1, -6.9200
21, 2, -6.9100
22, 2, -6.8500
23, 10, -6.6900
24, 2, -6.6800
25, 1, -6.6600
26, 20, -6.6500
How it would be possible to further customize my filter via modifying my AWK expression (or pipe it to something else) in order to consider additionally only the lines with small difference in the negative value of the third column, dG compared to the first line (which has the value most negative)? For example to consider only the lines different no more then 20% in terms of dG compared to the first line, while keeping all rest conditions the same:
ID, POP, dG
1, 7, -9.6000
2, 3, -8.7700
3, 6, -8.6200
4, 4, -8.2700
5, 6, -8.0800
6, 10, -8.0100
7, 9, -7.9700
8, 8, -7.8400
9, 16, -7.8100
10, 2, -7.7000
Both tasks can be done in a single awk:
awk -F ', ' 'NR==1 {next} FNR==NR {if (max < $2) {max=$2; n=FNR}; if (FNR==2) dg = $3 * .8; next} $3+0 == $3 && (FNR == n+1 || $3 > dg) {exit} 1' file file
ID, POP, dG
1, 7, -9.6000
2, 3, -8.7700
3, 6, -8.6200
4, 4, -8.2700
5, 6, -8.0800
6, 10, -8.0100
7, 9, -7.9700
8, 8, -7.8400
9, 16, -7.8100
10, 2, -7.7000
To make it more readable:
awk -F ', ' '
NR == 1 {
next
}
FNR == NR {
if (max < $2) {
max = $2
n = FNR
}
if (FNR == 2)
dg = $3 * .8
next
}
$3 + 0 == $3 && (FNR == n+1 || $3 > dg) {
exit
}
1' file file

Parse data from csv file into given format using Prolog

I have the csv file that contains data as:
A B C
A - 4 5
B 8 - 6
C 2 3 -
I want to have facts in the following form:
num(a,b,4).
num(a,c,5).
num(b,a,8).
num(b,c,6).
num(c,a,2).
num(c,b,3).
There should not be facts for similar alphabets like num(a,a,-).
I am using prolog's csv_read_file as:
csv_read_file(Path, Rows, [functor(num), arity(4)]), maplist(assert, Rows).
and its giving me output as:
Rows = [num('', 'A', 'B', 'C'), num('A', -, 4, 5), num('B', 8, -, 6), num('C', 2, 3, -)]
It seems to be a basic question, but I am not able to think about condition to perform this. Any help will be highly appreciated.
As per Isabelle Newbie Answer:
Open :- csv_read_file('Path', Rows, [functor(num), arity(4)]), table_entry(Rows, Row).
header_row_entry(Header,Row,Entry):-
arg(1, Row, RowName),
functor(Header, _, Arity),
between(2,Arity,ArgIndex),
arg(ArgIndex, Header, ColumnName),
arg(ArgIndex, Row, Value),
Entry = num(RowName, ColumnName, Value),
writeln(Entry).
table_entry(Entries, Entry):-
Entries = [Header | Rows],
member(Row, Rows),
header_row_entry(Header, Row, Entry).
Now, can anyone explain how and where I should use maplist to convert the rows in form of facts (neglect filtering of '-' and lowercase for now) so that when I query:
?-num(A,B,X).
I should get:
X=4
Next task is, I want to implement depth first search algorithm on it. Any details regarding this will be highly appreciated.
Consider a table header num('', 'A', 'B', 'C') and a row in the table num('B', 8, -, 6). From this you want to compute a table entry identified by the row's name, which here is 'B', and by a column name: the column name being 'A' for the first value (8), 'B' for the second (-), 'C' for the third (6).
Here's a simple way to do this, involving some typing and the obligatory copy-and-paste errors:
header_row_entry(Header, Row, Entry) :-
Header = num('', ColumnName, _, _),
Row = num(RowName, Value, _, _),
Entry = num(RowName, ColumnName, Value).
header_row_entry(Header, Row, Entry) :-
Header = num('', _, ColumnName, _),
Row = num(RowName, _, Value, _),
Entry = num(RowName, ColumnName, Value).
header_row_entry(Header, Row, Entry) :-
Header = num('', _, _, ColumnName),
Row = num(RowName, _, _, Value),
Entry = num(RowName, ColumnName, Value).
This enumerates all the entries in a row on backtracking:
?- Header = num('', 'A', 'B', 'C'), Row = num('B', 8, -, 6),
header_row_entry(Header, Row, Entry).
Header = num('', 'A', 'B', 'C'),
Row = num('B', 8, -, 6),
Entry = num('B', 'A', 8) ;
Header = num('', 'A', 'B', 'C'),
Row = num('B', 8, -, 6),
Entry = num('B', 'B', -) ;
Header = num('', 'A', 'B', 'C'),
Row = num('B', 8, -, 6),
Entry = num('B', 'C', 6).
To enumerate all the entries in an entire table, it remains to enumerate all rows, then enumerate row entries as above. Here this is:
table_entry(Entries, Entry) :-
Entries = [Header | Rows],
member(Row, Rows),
header_row_entry(Header, Row, Entry).
And now, given your table:
?- Table = [num('', 'A', 'B', 'C'), num('A', -, 4, 5), num('B', 8, -, 6), num('C', 2, 3, -)], table_entry(Table, Entry).
Table = [num('', 'A', 'B', 'C'), num('A', -, 4, 5), num('B', 8, -, 6), num('C', 2, 3, -)],
Entry = num('A', 'A', -) ;
Table = [num('', 'A', 'B', 'C'), num('A', -, 4, 5), num('B', 8, -, 6), num('C', 2, 3, -)],
Entry = num('A', 'B', 4) ;
Table = [num('', 'A', 'B', 'C'), num('A', -, 4, 5), num('B', 8, -, 6), num('C', 2, 3, -)],
Entry = num('A', 'C', 5) ;
Table = [num('', 'A', 'B', 'C'), num('A', -, 4, 5), num('B', 8, -, 6), num('C', 2, 3, -)],
Entry = num('B', 'A', 8) ;
Table = [num('', 'A', 'B', 'C'), num('A', -, 4, 5), num('B', 8, -, 6), num('C', 2, 3, -)],
Entry = num('B', 'B', -) . % etc.
Depending on what you want exactly, it remains to lowercase the row and column names (the irritatingly named downcase_atom in SWI-Prolog, for example) and filter out the - entries. You can then assert the entries using a failure-driven loop or by collecting all of them using findall and asserting using maplist.
Now that we have a working solution, we might want header_row_entry to be a bit nicer. We can use arg/3 to capture more explicitly that we are trying to pair a column name and a value that are at the same argument position in their respective header and row terms:
header_row_entry(Header, Row, Entry) :-
arg(1, Row, RowName),
functor(Header, _, Arity),
between(2, Arity, ArgIndex),
arg(ArgIndex, Header, ColumnName),
arg(ArgIndex, Row, Value),
Entry = num(RowName, ColumnName, Value).
This is shorter than the above and applicable to any number of columns in the table.

Error in my Json file (Error: Parse error on line 176: ...1, "furnace": 1, }, "ItemListCrates" ---------------------^ Expecting 'STRING', got '}

Well i have a issue somewhere in my code, this is not my cup of tea to say to fix this. Ive ran through this with a fine tooth brush practically to no luck, and thought someone could tell me where on my code im doing wrong on. Code error is this once again Error: Parse error on line 176:
...1, "furnace": 1, }, "ItemListCrates"
---------------------^
Expecting 'STRING', got '}
{
"ItemListBarrels": {
"rifle.ak": 4,
"ammo.handmade.shell": 1,
"ammo.pistol": 2,
"ammo.pistol.fire": 2,
"ammo.pistol.hv": 2,
"ammo.rifle": 3,
"ammo.rifle.explosive": 2,
"ammo.rifle.incendiary": 2,
"ammo.rifle.hv": 2,
"ammo.rocket.basic": 4,
"ammo.rocket.fire": 4,
"ammo.rocket.hv": 4,
"ammo.shotgun": 4,
"ammo.shotgun.slug": 2,
"antiradpills": 1,
"apple": 1,
"arrow.hv": 1,
"axe.salvaged": 2,
"barricade.concrete": 1,
"barricade.metal": 1,
"barricade.sandbags": 1,
"barricade.stone": 1,
"barricade.wood": 1,
"barricade.woodwire": 1,
"tool.binoculars": 1,
"black.raspberries": 4,
"bleach": 1,
"blueberries": 4,
"bone.club": 1,
"bucket.water": 1,
"tool.camera": 1,
"can.beans": 1,
"can.tuna": 1,
"candycane": 1,
"ceilinglight": 1,
"chair": 1,
"chocholate": 1,
"door.double.hinged.metal": 1,
"door.double.hinged.toptier": 1,
"door.double.hinged.wood": 1,
"door.hinged.toptier": 1,
"door.closer": 1,
"dropbox": 1,
"explosive.satchel": 4,
"explosive.timed": 4,
"explosives": 4,
"floor.grill": 1,
"floor.ladder.hatch": 1,
"fridge": 1,
"furnace.large": 1,
"gates.external.high.stone": 1,
"gates.external.high.wood": 1,
"gears": 2,
"burlap.gloves": 1,
"glue": 1,
"granolabar": 1,
"grenade.beancan": 4,
"fun.guitar": 1,
"hammer.salvaged": 2,
"hat.beenie": 1,
"hat.boonie": 1,
"bucket.helmet": 1,
"hat.candle": 1,
"hat.cap": 1,
"coffeecan.helmet": 2,
"hat.miner": 1,
"hatchet": 2,
"hazmatsuit": 3,
"hoodie": 2,
"icepick.salvaged": 2,
"jacket.snow": 1,
"jacket": 1,
"ladder.wooden.wall": 1,
"lantern": 1,
"largemedkit": 2,
"locker": 1,
"longsword": 1,
"mace": 1,
"machete": 1,
"mailbox": 1,
"map": 1,
"mask.balaclava": 1,
"mask.bandana": 1,
"metal.facemask": 1,
"metal.plate.torso": 1,
"metalblade": 2,
"metalpipe": 2,
"mining.quarry": 1,
"burlap.trousers": 1,
"pants": 1,
"roadsign.kilt": 3,
"pants.shorts": 1,
"pickaxe": 2,
"pistol.eoka": 1,
"pistol.revolver": 2,
"planter.large": 1,
"planter.small": 1,
"propanetank": 1,
"target.reactive": 1,
"riflebody": 3,
"roadsign.jacket": 2,
"roadsigns": 2,
"rope": 1,
"rug.bear": 1,
"rug": 1,
"salvaged.cleaver": 1,
"salvaged.sword": 1,
"weapon.mod.small.scope": 1,
"scrap": 1000,
"searchlight": 1,
"semibody": 2,
"sewingkit": 1,
"sheetmetal": 1,
"shelves": 1,
"shirt.collared": 1,
"shirt.tanktop": 1,
"shoes.boots": 1,
"shotgun.waterpipe": 2,
"guntrap": 1,
"shutter.metal.embrasure.a": 1,
"shutter.metal.embrasure.b": 1,
"shutter.wood.a": 1,
"sign.hanging.banner.large": 1,
"sign.hanging": 1,
"sign.hanging.ornate": 1,
"sign.pictureframe.landscape": 1,
"sign.pictureframe.portrait": 1,
"sign.pictureframe.tall": 1,
"sign.pictureframe.xl": 1,
"sign.pictureframe.xxl": 1,
"sign.pole.banner.large": 1,
"sign.post.double": 1,
"sign.post.single": 1,
"sign.post.town": 1,
"sign.post.town.roof": 1,
"sign.wooden.huge": 1,
"sign.wooden.large": 1,
"sign.wooden.medium": 1,
"sign.wooden.small": 1,
"weapon.mod.silencer": 1,
"weapon.mod.simplesight": 1,
"small.oil.refinery": 1,
"smallwaterbottle": 1,
"smgbody": 2,
"spear.stone": 1,
"spikes.floor": 1,
"spinner.wheel": 1,
"metalspring": 1,
"sticks": 2,
"surveycharge": 2,
"syringe.medical": 3,
"table": 1,
"techparts": 3,
"smg.thompson": 2,
"tshirt": 1,
"tshirt.long": 1,
"tunalight": 1,
"wall.external.high.stone": 1,
"wall.external.high": 1,
"wall.frame.cell.gate": 1,
"wall.frame.cell": 1,
"wall.frame.fence.gate": 1,
"wall.frame.fence": 1,
"wall.frame.netting": 1,
"wall.frame.shopfront": 1,
"wall.window.bars.metal": 1,
"wall.window.bars.toptier": 1,
"wall.window.bars.wood": 1,
"water.catcher.large": 1,
"water.catcher.small": 1,
"water.barrel": 1,
"waterjug": 1,
"water.purifier": 1,
"furnace": 1,
},
"ItemListCrates": {
"rifle.ak": 4,
"ammo.handmade.shell": 1,
"ammo.pistol": 2,
"ammo.pistol.fire": 2,
"ammo.pistol.hv": 2,
"ammo.rifle": 3,
"ammo.rifle.explosive": 2,
"ammo.rifle.incendiary": 2,
"ammo.rifle.hv": 2,
"ammo.rocket.basic": 4,
"ammo.rocket.fire": 4,
"ammo.rocket.hv": 4,
"ammo.shotgun": 4,
"ammo.shotgun.slug": 2,
"antiradpills": 1,
"apple": 1,
"arrow.hv": 1,
"axe.salvaged": 2,
"barricade.concrete": 1,
"barricade.metal": 1,
"barricade.sandbags": 1,
"barricade.stone": 1,
"barricade.wood": 1,
"barricade.woodwire": 1,
"tool.binoculars": 1,
"black.raspberries": 4,
"bleach": 1,
"blueberries": 4,
"bone.club": 1,
"bucket.water": 1,
"tool.camera": 1,
"can.beans": 1,
"can.tuna": 1,
"candycane": 1,
"ceilinglight": 1,
"chair": 1,
"chocholate": 1,
"door.double.hinged.metal": 1,
"door.double.hinged.toptier": 1,
"door.double.hinged.wood": 1,
"door.hinged.toptier": 1,
"door.closer": 1,
"dropbox": 1,
"explosive.satchel": 4,
"explosive.timed": 4,
"explosives": 4,
"floor.grill": 1,
"floor.ladder.hatch": 1,
"fridge": 1,
"furnace.large": 1,
"gates.external.high.stone": 1,
"gates.external.high.wood": 1,
"gears": 2,
"burlap.gloves": 1,
"glue": 1,
"granolabar": 1,
"grenade.beancan": 4,
"fun.guitar": 1,
"hammer.salvaged": 2,
"hat.beenie": 1,
"hat.boonie": 1,
"bucket.helmet": 1,
"hat.candle": 1,
"hat.cap": 1,
"coffeecan.helmet": 2,
"hat.miner": 1,
"hatchet": 2,
"hazmatsuit": 3,
"hoodie": 2,
"icepick.salvaged": 2,
"jacket.snow": 1,
"jacket": 1,
"ladder.wooden.wall": 1,
"lantern": 1,
"largemedkit": 2,
"locker": 1,
"longsword": 1,
"mace": 1,
"machete": 1,
"mailbox": 1,
"map": 1,
"mask.balaclava": 1,
"mask.bandana": 1,
"metal.facemask": 1,
"metal.plate.torso": 1,
"metalblade": 2,
"metalpipe": 2,
"mining.quarry": 1,
"burlap.trousers": 1,
"pants": 1,
"roadsign.kilt": 3,
"pants.shorts": 1,
"pickaxe": 2,
"pistol.eoka": 1,
"pistol.revolver": 2,
"planter.large": 1,
"planter.small": 1,
"propanetank": 1,
"target.reactive": 1,
"riflebody": 3,
"roadsign.jacket": 2,
"roadsigns": 2,
"rope": 1,
"rug.bear": 1,
"rug": 1,
"salvaged.cleaver": 1,
"salvaged.sword": 1,
"weapon.mod.small.scope": 1,
"scrap": 700,
"searchlight": 1,
"semibody": 2,
"sewingkit": 1,
"sheetmetal": 1,
"shelves": 1,
"shirt.collared": 1,
"shirt.tanktop": 1,
"shoes.boots": 1,
"shotgun.waterpipe": 2,
"guntrap": 1,
"shutter.metal.embrasure.a": 1,
"shutter.metal.embrasure.b": 1,
"shutter.wood.a": 1,
"sign.hanging.banner.large": 1,
"sign.hanging": 1,
"sign.hanging.ornate": 1,
"sign.pictureframe.landscape": 1,
"sign.pictureframe.portrait": 1,
"sign.pictureframe.tall": 1,
"sign.pictureframe.xl": 1,
"sign.pictureframe.xxl": 1,
"sign.pole.banner.large": 1,
"sign.post.double": 1,
"sign.post.single": 1,
"sign.post.town": 1,
"sign.post.town.roof": 1,
"sign.wooden.huge": 1,
"sign.wooden.large": 1,
"sign.wooden.medium": 1,
"sign.wooden.small": 1,
"weapon.mod.silencer": 1,
"weapon.mod.simplesight": 1,
"small.oil.refinery": 1,
"smallwaterbottle": 1,
"smgbody": 2,
"spear.stone": 1,
"spikes.floor": 1,
"spinner.wheel": 1,
"metalspring": 1,
"sticks": 2,
"surveycharge": 2,
"syringe.medical": 3,
"table": 1,
"techparts": 3,
"smg.thompson": 2,
"tshirt": 1,
"tshirt.long": 1,
"tunalight": 1,
"wall.external.high.stone": 1,
"wall.external.high": 1,
"wall.frame.cell.gate": 1,
"wall.frame.cell": 1,
"wall.frame.fence.gate": 1,
"wall.frame.fence": 1,
"wall.frame.netting": 1,
"wall.frame.shopfront": 1,
"wall.window.bars.metal": 1,
"wall.window.bars.toptier": 1,
"wall.window.bars.wood": 1,
"water.catcher.large": 1,
"water.catcher.small": 1,
"water.barrel": 1,
"waterjug": 1,
"water.purifier": 1,
"rocket.launcher": 4,
"flamethrower": 2,
"flameturret": 2,
}
}
There is no comma allowed after the last item of a Json-object:
"water.purifier": 1,
"furnace": 1, //<--Remove this comma
},
"flamethrower": 2,
"flameturret": 2,//<--Remove this comma
}

SQL query INSERT column count error

I have removed 'age_range' from this query.
INSERT INTO `filters` (`id`, `user_id`, `profession_preference`, `country`, `city`, `order_by`, `profession_orientation`, `age_range`, `distance_range`, `location_dating`) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);
I executed again and receive this error:
MySQL said: Documentation
#1136 - Column count doesn't match value count at row 1
When you insert a record, it matches the values in the VALUES list to the columns in the columns list by comma-separated position. So, this insert statement:
INSERT INTO `foo` (`A`, `B`, `C`)
VALUES ('valueA', 'valueB', 'valueC')
It will insert valueA into column A, valueB into column B, etc. because they match positions in their respective lists. If you remove B from the columns list and leave VALUES alone, it will not attempt to insert valueA into column A, but valueB into column C because they now match value positions, but it won't know what to do with valueC because there are now more values than columns, so since you removed the column from the second position, you would also need to remove the value from the second position.
So back to your query, you would need to determine which position age_range occupied in the columns list and remove the values from the same position in the values list.
Does that make sense?
You have 9 columns defined in your insert statement and you are trying to insert 10 values. you either need to add another column definition or remove from your values.
According to rule the column name define and provided values count should be same. In your case one column value in extra.
As the documentation says
"Column count doesn't match value count"
You specify 9 columns (id, user_id, profession_preference, country, city, order_by, profession_orientation, distance_range, location_dating) in your insert statement
and you are trying to insert 10 values.
You have to remove one value or add another column
Before removing column this is the script which will work
CREATE TABLE filters (id INT, user_id INT, profession_preference INT, country VARCHAR(50), city VARCHAR(50), order_by INT, profession_orientation INT, age_range VARCHAR(50), distance_range VARCHAR(50), location_dating INT);
INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, age_range, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '16,100', '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '18,23', '0,500 ', 0),
(26, 316, 3, 'United States', '', 3, 1, '17,25', '0,500', 0);
Now since you removed age_range column, below script will work:
INSERT INTO filters (id, user_id, profession_preference, country, city, order_by, profession_orientation, distance_range, location_dating) VALUES
(9, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(10, 12, 3, 'Egypt', '', 2, 1, '', 0),
(11, 19, 3, 'All Countries', '', 2, 1, '0,500', 0),
(13, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(14, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(15, 20, 3, 'All Countries', '', 2, 1, '0,500', 0),
(25, 121, 3, 'All Countries', '', 3, 1, '0,500', 0),
(26, 316, 3, 'United States', '', 3, 1, '0,500', 0);
I removed third last column from insert script.
Hope this helps!

combine multiple rows from the same table (days of the week)

I have a table which stores yes/no values for each hour of the day for a particular user/filter/type. Each day is its own row. So there will always be 7 rows for any given user/filter/type combination.
What I am trying to accomplish is one result for each user/filter/type combination that contains all hours of each day of the week. I think the approach I need here is self joining with aliases and groups, but everything I have tried fails. I have setup a basic fiddle at fiddle
I also have the ability to change the db schema for this as well if there is an easier and/or preferred method to handle this on the db side (my gut says there is). Perhaps a table for each day of the week linked by filter_id?
INSERT INTO filters
(`filter_id`, `user_id`, `filter`, `type`, `day`, `12a`, `1a`... and so on for each hour)
VALUES
(1, 1, 'filter1', 1, 1, 1, 1),
(2, 1, 'filter1', 1, 2, 1, 1),
(3, 1, 'filter1', 1, 3, 1, 1),
(4, 1, 'filter1', 1, 4, 1, 1),
(5, 1, 'filter1', 1, 5, 1, 1),
(6, 1, 'filter1', 1, 6, 1, 1),
(7, 1, 'filter1', 1, 7, 1, 1),
(8, 1, 'filter2', 1, 1, 0, 0),
(9, 1, 'filter2', 1, 2, 0, 0),
(10, 1, 'filter2', 1, 3, 0, 0),
(11, 1, 'filter2', 1, 4, 0, 0),
(12, 1, 'filter2', 1, 5, 0, 0),
(13, 1, 'filter2', 1, 6, 0, 0),
(14, 1, 'filter2', 1, 7, 0, 0),
(15, 1, 'filter3', 1, 1, 0, 0),
(16, 1, 'filter3', 1, 2, 0, 0),
(17, 1, 'filter3', 1, 3, 0, 0),
(18, 1, 'filter3', 1, 4, 0, 0),
(19, 1, 'filter3', 1, 5, 0, 0),
(20, 1, 'filter3', 1, 6, 0, 0),
(21, 1, 'filter3', 1, 7, 0, 0)
;
EDIT :
I made some progress on this and it is showing all day hours for the filter in one result...however... I it does not work when a user has more than one filter. I can't seem to get the grouping correct and/or something else so results only show unique user/type/filter combinations... at the moment it only shows one filter result for each user.
This is only joining monday, tuesday, wednesday as well... there must be an easier way to do this. Like I said I am totally open up to changing the schema of the db for this as well, but not sure what the best approach would be other than this. I certainly cannot list all the hours for the entire week in one table (that would be 168 columns for hours plus an additional for for 172 in each row).
$stmt = $db->prepare("
SELECT users.user_id, users.username, c.computer_name, filters.user_id, filters.filter, filters.type,
monday.12a as m12a,
monday.1a as m1a,
monday.2a as m2a,
monday.3a as m3a,
monday.4a as m4a,
monday.5a as m5a,
monday.6a as m6a,
monday.7a as m7a,
monday.8a as m8a,
monday.9a as m9a,
monday.10a as m10a,
monday.11a as m11a,
monday.12p as m12p,
monday.1p as m1p,
monday.2p as m2p,
monday.3p as m3p,
monday.4p as m4p,
monday.5p as m5p,
monday.6p as m6p,
monday.7p as m8p,
monday.9p as m9p,
monday.10p as m10p,
monday.11p as m11p,
tuesday.12a as t12a,
tuesday.1a as t1a,
tuesday.2a as t2a,
tuesday.3a as t3a,
tuesday.4a as t4a,
tuesday.5a as t5a,
tuesday.6a as t6a,
tuesday.7a as t7a,
tuesday.8a as t8a,
tuesday.9a as t9a,
tuesday.10a as t10a,
tuesday.11a as t11a,
tuesday.12p as t12p,
tuesday.1p as t1p,
tuesday.2p as t2p,
tuesday.3p as t3p,
tuesday.4p as t4p,
tuesday.5p as t5p,
tuesday.6p as t6p,
tuesday.7p as t8p,
tuesday.9p as t9p,
tuesday.10p as t10p,
tuesday.11p as t11p,
wednesday.12a as w12a,
wednesday.1a as w1a,
wednesday.2a as w2a,
wednesday.3a as w3a,
wednesday.4a as w4a,
wednesday.5a as w5a,
wednesday.6a as w6a,
wednesday.7a as w7a,
wednesday.8a as w8a,
wednesday.9a as w9a,
wednesday.10a as w10a,
wednesday.11a as w11a,
wednesday.12p as w12p,
wednesday.1p as w1p,
wednesday.2p as w2p,
wednesday.3p as w3p,
wednesday.4p as w4p,
wednesday.5p as w5p,
wednesday.6p as w6p,
wednesday.7p as w8p,
wednesday.9p as w9p,
wednesday.10p as w10p,
wednesday.11p as w11p
FROM
( SELECT account_id, computer_id, computer_name
FROM computers
WHERE account_id = 1
ORDER BY computer_id ASC LIMIT 0, 5
) as c
LEFT JOIN users
on users.computer_id = c.computer_id
LEFT JOIN filters
on filters.user_id = users.user_id
LEFT JOIN filters as monday
on monday.user_id = filters.user_id and monday.filter = filters.filter and monday.day = 1
LEFT JOIN filters as tuesday
on tuesday.user_id = filters.user_id and tuesday.filter = filters.filter and tuesday.day = 2
LEFT JOIN filters as wednesday
on wednesday.user_id = filters.user_id and wednesday.filter = filters.filter and wednesday.day = 3
WHERE filters.type = 1
GROUP BY users.user_id
");
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Here is the official mysql page. http://dev.mysql.com/doc/refman/5.0/en/join.html