Make a SQL query on three tables with a NOT-LIKE clause - mysql

I need to extract all servers that are not member of a specified group.
I have 3 tables: host (containing hosts), hostgroup_relation (containing the host id and the hostgroup id), hostgroup (containing hostgroups)
I can get the relationships, but I need every hosts that are NOT member of the group with id 180
Host:
SELECT host_id,host_name FROM host LIMIT 10;
+---------+-------------------+
| host_id | host_name |
+---------+-------------------+
| 1482 | AADSYNC1 |
| 442 | Acces-Point-Wifi |
| 1916 | ADAUDIT1 |
| 1562 | ADMORA1 |
| 2247 | ADMRDS2 |
| 2226 | ADSECU1 |
| 1203 | ADSELFSERVICE1 |
| 1172 | ALFRESCO1 |
| 1841 | ALFRESCO2 |
| 172 | Antispam-Ironport |
+---------+-------------------+
Hostgroups:
SELECT hg_id, hg_name FROM hostgroup LIMIT 10
+-------+----------------------+
| hg_id | hg_name |
+-------+----------------------+
| 82 | Antivirus-Trend |
| 65 | Autocoms |
| 72 | Baies-de-stockage |
| 78 | Consoles |
| 192 | Databases-All |
| 193 | Databases-Main |
| 68 | Databases-MySql |
| 67 | Databases-Oracle |
| 181 | Databases-PostgreSql |
| 69 | Databases-SQLServer |
+-------+----------------------+
Host/hostgroup relation:
SELECT * FROM hostgroup_relation LIMIT 10;
+--------+-----------------+--------------+
| hgr_id | hostgroup_hg_id | host_host_id |
+--------+-----------------+--------------+
| 5698 | 70 | 1167 |
| 6772 | 53 | 1167 |
| 6820 | 144 | 1369 |
| 6821 | 62 | 1369 |
| 6822 | 53 | 1369 |
| 6823 | 70 | 1369 |
| 6825 | 62 | 1370 |
| 6826 | 53 | 1370 |
| 6827 | 70 | 1370 |
| 6829 | 62 | 1371 |
+--------+-----------------+--------------+
Here is where I've gone so far:
SELECT host.host_name, hostgroup.hg_name
FROM host, hostgroup_relation, hostgroup
WHERE hostgroup_relation.hostgroup_hg_id = hostgroup.hg_id
AND hostgroup_relation.host_host_id = host.host_id
LIMIT 10;
+-----------+-----------------------------+
| host_name | hg_name |
+-----------+-----------------------------+
| AADSYNC1 | Default-bi |
| AADSYNC1 | Serveurs-Virtuels |
| AADSYNC1 | Serveurs-Windows |
| AADSYNC1 | Reboot_serveurs-12h00:14h00 |
| ADAUDIT1 | Default-bi |
| ADAUDIT1 | Serveurs-Virtuels |
| ADAUDIT1 | Serveurs-Windows |
| ADAUDIT1 | Reboot_serveurs-12h00:14h00 |
| ADMORA1 | Default-bi |
| ADMORA1 | Reboot_serveurs-00h00:4h00 |
+-----------+-----------------------------+
And I need a list of all servers that are not in a specified group.

You need to do a simple join.
This could work:
SELECT h.host_id, h.host_name
FROM host h
LEFT OUTER JOIN hostgroup_relation hgr ON (hgr.host_host_id = h.host_id AND hgr.hostgroup_hg_id = 180)
WHERE hgr.hgr_id IS NULL

Try this:
select h.host_id, hg.host_name, hg.hg_id
from host h
join hostgroup_relation hg
where h.host_id = hg.host_host_id
and hg_id not in 180;

Related

not able to fetch date value using subqueries mysql

i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;

convert rows into coumns dinamiacally for some table with CONCAT and GROUP_CONCAT mysql query

by followed Mysql query to dynamically convert rows to columns on the basis of two columns,
I have 4 table to convert dinamiacally rows into columns as RESULT
TABLE A
| id | stages |
-----+----------
| 1 | Stage A |
| 2 | Stage B |
| 3 | Stage C |
TABLE B
| id_b | code | lessons |
-------+------+--------------
| 10 | FIS | Physics |
| 11 | MAT | Mathematics |
| 12 | KIM | Chemistry |
| 13 | BIO | Biology |
TABLE C
|id_c| students |
-----+-------------
| 20 | student 20 |
| 21 | student 21 |
| 22 | student 22 |
| 23 | student 23 |
| 24 | student 24 |
| 25 | student 25 |
| 26 | student 26 |
| 27 | student 27 |
TABLE D
| id_d | id_a | id_b | id_c | value_m | value_n |
-------+------+------+------+---------+----------
| 201 | 1 | 11 | 20 | 71 | 73 |
| 203 | 1 | 13 | 20 | 80 | 87 |
| 204 | 2 | 10 | 21 | 72 | 75 |
| 206 | 2 | 13 | 21 | 76 | 78 |
| 208 | 2 | 12 | 22 | 72 | 78 |
| 209 | 2 | 13 | 22 | 76 | 80 |
| 210 | 3 | 11 | 23 | 73 | 71 |
| 211 | 3 | 12 | 23 | 71 | 77 |
| 212 | 3 | 13 | 23 | 78 | 81 |
| 213 | 3 | 10 | 24 | 70 | 81 |
| 214 | 3 | 11 | 25 | 71 | 82 |
| 215 | 3 | 12 | 26 | 74 | 83 |
| 215 | 3 | 13 | 27 | 78 | 80 |
RESULT
| students | FIS_1 | MAT_1 | KIM_1 | BIO_1 | FIS_2 | MAT_2 | KIM_2 | BIO_2 | FIS_3 | MAT_3 | KIM_3 | BIO_3 |
-------------+-------+-------+-------+-------+--------+------+-------+-------+-------+-------+-------+--------
| student 20 | | 71,73 | | 80,87 | | | | | | | | |
| student 21 | | | | | 72,75 | 76,78 | | | | | | |
| student 22 | | | | | | | 72,78 | 76,80 | | | | |
| student 23 | | | | | | | | | | 73,71 | 71,77 | 78,81 |
| student 24 | | | | | | | | | 70,81 | | | |
| student 25 | | | | | | | | | | 71,82 | | |
| student 26 | | | | | | | | | | | 74,83 | |
| student 27 | | | | | | | | | | | | 78,80 |
Where FIS_1, MAT_1,... FIS_2, MAT_2.., FIS_3, MAT_3.. is CONCAT from TABLE B kode and TABLE A id,
and value MAT_1, BIO_1... is CONCAT from TABLE D value_m and value_n
It's Posible? How can I do that with GROUP_CONCAT mysql query? I've tried it but still not successful..
Thanks for your advance..

GROUP BY doesn't work

please advice why group by doesn't work here.
I want it to be grouped by setup_time:
mysql> select i_connection, avg(setup_time) from CDR_Vendors where i_env = 9 group by i_connection order by setup_time asc limit 15;
+--------------+-----------------+
| i_connection | avg(setup_time) |
+--------------+-----------------+
| 0 | 0.0000 |
| 351 | 0.0000 |
| 235 | 11.9830 |
| 376 | 77.3151 |
| 396 | 168.3036 |
| 318 | 99.0741 |
| 319 | 126.4138 |
| 317 | 366.6881 |
| 545 | 2523.9878 |
| 551 | 4811.5482 |
| 485 | 3410.2093 |
| 289 | 692.0000 |
| 239 | 4314.1667 |
| 510 | 3322.8444 |
| 546 | 2041.7491 |
+--------------+-----------------+
select i_connection, avg(setup_time) from CDR_Vendors where i_env = 9 group by setup_time order by setup_time asc limit 15;
You can group by 'i_connection'.

MySQL information from another table

I have the following SQL:
Select roleid , deity_level FROM default_jd_deity_role LIMIT 10;
That gives the output:
+--------+-------------+
| roleid | deity_level |
+--------+-------------+
| 1024 | 1 |
| 1043 | 54 |
| 1056 | 1 |
| 1057 | 54 |
| 1072 | 54 |
| 1074 | 45 |
| 1075 | 36 |
| 1088 | 45 |
| 1089 | 45 |
| 1104 | 27 |
+--------+-------------+
Then I have this SQL:
Select roleid , name FROM default_jd_ingame_roles LIMIT 22, 10
That gives the following output:
+--------+---------+
| roleid | name |
+--------+---------+
| 1024 | Hulu |
| 1043 | Cookiez |
| 1056 | Sam |
| 1057 | Sugar |
| 1072 | Leah |
| 1073 | Smexy |
| 1074 | Bam! |
| 1075 | Lexi |
| 1088 | OneShot |
| 1089 | Demono |
+--------+---------+
What I am trying to do is make deity_level add on to the second SQL Query like this:
+--------+---------+-------------+
| roleid | name | deity_level |
+--------+---------+-------------+
| 1024 | Hulu | 1 |
| 1043 | Cookiez | 54 |
| 1056 | Sam | 1 |
| 1057 | Sugar | 54 |
| 1072 | Leah | 54 |
| 1073 | Smexy | 45 |
| 1074 | Bam! | 36 |
| 1075 | Lexi | 45 |
| 1088 | OneShot | 45 |
| 1089 | Demono | 27 |
+--------+---------+-------------+
Try this:
Select a.roleid , a.deity_level, b.name
FROM default_jd_deity_role AS a
JOIN default_jd_ingame_roles AS b ON a.roleid=b.roleid
LIMIT 10
You can achieve this with a JOIN, like this:
SELECT main.roleid, roles.name, main.deity_level
FROM default_jd_deity_role main
LEFT JOIN default_jd_ingame_roles roles ON main.roleid = roles.roleid
LIMIT 22, 10
I'll leave two nice tutorials to get you started on JOINS:
http://www.sitepoint.com/understanding-sql-joins-mysql-database/
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
You are looking for a JOIN query:
SELECT igr.roleid , igr.name, dr.diety_level
FROM default_jd_ingame_roles igr
JOIN default_jd_deity_role dr ON dr.roleid = igr.roleid

mysql group_concat comma separated ids to match names

I wrote this query to find the list of shops selling what categories.
SELECT GROUP_CONCAT(distinct(sub_category_id)) AS s,
vendor_id AS v
FROM link_products_lists_vendors
GROUP BY vendor_id;
This results in,
+---------------------------------------+------------+
| category_ids | vendor_ids |
+---------------------------------------+------------+
| 24,28,25,16,26,23,27,2 | 3 |
| 2 | 67 |
| 19,28,17,16,20,2 | 68 |
| 19,28,24,26,23,21,16,27,22,17,25,2 | 122 |
| 16,2 | 123 |
| 28,17,22,21,18,16,26,27,20,23,25,2 | 124 |
| 22,19,21,20,16,24,28,25,23,26,2 | 125 |
| 23,24,26,25,28,16,20,27,19,2 | 126 |
| 19,26,28,18,20,27,22,16 | 127 |
| 22,26,28,21,23,20,24,19,16,17,27,25,2 | 128 |
| 2 | 129 |
| 2 | 133 |
| 19,20,28,16,27,25,21,23,26,24,22 | 135 |
| 23,28,17,22,26,21,16,20,27,24,25,2 | 136 |
| 19,17,16,21,23,26,22,25,27,20,28 | 137 |
| 19,20,26,22,21,24,23,17,28,16,27,25,2 | 138 |
| 19,20,23,28,26,21,24,16,27,22,25,17,2 | 139 |
| 22,27,20,21,24,17,23,28,26,19,25,2 | 142 |
| 19,28,17,20,2 | 143 |
+---------------------------------------+------------+
19 rows in set (0.01 sec)
What I want now is something like,
+-------------------------- -----------+--- ----------+
| category_names | vendor_names |
+---------------------------------------+--------------+
| mobiles,laptops,desktops | abcShop |
| mobiles | xyzShop |
| desktops,mouses,keyboards | pqrShop |
+---------------------------------------+--------------+
I have the categories table as,
+----+---------------+
| id | name |
+----+---------------+
| 17 | desktops |
| 18 | external_hdds |
| 26 | headphones |
| 27 | headsets |
| 22 | keyboards |
| 16 | laptops |
| 24 | memory_cards |
| 2 | mobile-phones |
| 21 | mouses |
| 25 | pendrives |
| 19 | printers |
| 20 | routers |
| 23 | speakers |
| 28 | tablets |
+----+---------------+
Vendors table as,
+-----+----------------------+
| id | name |
+-----+----------------------+
| 108 | abcShop |
| 109 | xyzShop |
| 45 | pqrShop |
| 89 | . |
| 63 | . |
| 64 | . |
+-----+----------------------+
How should I write a query that will not display ids but use the table that displays ids and displays names? I have no clue from where to start this. Please help!
Just join in the other tables and select those values instead
SELECT GROUP_CONCAT(distinct(c.name)) AS s,
v.name AS v
FROM link_products_lists_vendors l
JOIN categories c on l.category_id = c.id
JOIN vendors v on l.vendor_id = v.id
GROUP BY v.name;
This works as long as vendor names and description names are unique.