I have a below mysql query. It's returns wrong value. Please HELP ME to resolve this issue.
SELECT
T1.PARENT_ID,
GROUP_CONCAT(
IF(T2.PROPERTIES IS NULL, "NA", T2.PROPERTIES)
ORDER BY T1.ORDER_INDEX ASC
) AS DATA
FROM TABLE_1 T1
JOIN TABLE_2 T2
ON T1.ID=T2.ID
WHERE T1.AUTHOR="123"
GROUP BY T1.PARENT_ID
ORDER BY T1.PARENT_ID;
Mysql Version ===> 5.0.27-standard-log
The above query returns:
+-----------+---------------+
| parent_id | data |
+-----------+---------------+
| 12345 | te,test1,test |
| 23456 | NA |
+-----------+---------------+
2 rows in set (0.00 sec)
_
But, it should be:
+-----------+---------------+
| parent_id | data |
+-----------+---------------+
| 12345 | NA,test1,test |
| 23456 | NA |
+-----------+---------------+
2 rows in set (0.00 sec)
-
-
########################### TABLE DATA ############################
mysql> select * from Table_1;
+----+-----------+--------+-------------+
| id | parent_id | author | order_index |
+----+-----------+--------+-------------+
| 1 | 12345 | 123 | 3 |
| 2 | 12345 | 123 | 1 |
| 3 | 23456 | 123 | 1 |
| 4 | 12345 | 123 | 2 |
+----+-----------+--------+-------------+
4 rows in set (0.00 sec)
mysql> select * from Table_2;
+----+------------+
| id | properties |
+----+------------+
| 1 | test |
| 2 | NULL |
| 3 | NULL |
| 4 | test1 |
+----+------------+
4 rows in set (0.00 sec)
-
-
########################### TABLE MODEL ############################
mysql> desc Table_1;
+-------------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | | |
| parent_id | char(100) | YES | | NULL | |
| author | char(100) | YES | | NULL | |
| order_index | int(11) | YES | | 0 | |
+-------------+-----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> desc Table_2;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | | |
| properties | text | YES | | NULL | |
+------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
Related
I don't know how to explain my problem better than just showing the code and the error.
The following query is a minimal example for the problem I have encountered
UPDATE stops AS stops1 SET real_stop_id = (
SELECT MIN(served_by2.stop_id)
FROM served_by AS served_by1
INNER JOIN served_by AS served_by2
USING(route_short_name)
WHERE served_by1.stop_id = stops.stop_id
);
It returns the following error.
ERROR 1054 (42S22): Unknown column 'stops.stop_id' in 'where clause'
I'm using the timetable data for my city's tram network in the Google Transit Feed Specification format
https://developers.google.com/transit/gtfs/reference
In addition to this I have created and populated a table served_by.
mysql> describe served_by;
+------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+-------+
| stop_id | int(11) | YES | | NULL | |
| route_short_name | text | YES | | NULL | |
+------------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from served_by limit 5;
+---------+------------------+
| stop_id | route_short_name |
+---------+------------------+
| 378 | 19 |
| 378 | 19 |
| 378 | 19 |
| 378 | 19 |
| 398 | 5 |
+---------+------------------+
5 rows in set (0.06 sec)
For completeness, here is the stops table, which follows the GTFS format except for the column real_stop_id (which has no data), that I am currently trying to populate.
mysql> describe stops;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| stop_id | int(11) | YES | MUL | NULL | |
| real_stop_id | int(11) | YES | | NULL | |
| stop_no | varchar(30) | YES | MUL | NULL | |
| stop_name | varchar(150) | YES | MUL | NULL | |
| stop_lat | double | YES | | NULL | |
| stop_lon | double | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> select * from stops limit 5;
+----+---------+--------------+---------+--------------------------------------------+-------------------+------------------+
| id | stop_id | real_stop_id | stop_no | stop_name | stop_lat | stop_lon |
+----+---------+--------------+---------+--------------------------------------------+-------------------+------------------+
| 1 | 10311 | 947 | 45 | 45-Glenferrie Rd/Wattletree Rd (Malvern) | -37.862296736384 | 145.028194441473 |
| 2 | 10371 | 946 | 44 | 44-Duncraig Ave/Wattletree Rd (Armadale) | -37.8618932396197 | 145.025090664641 |
| 3 | 1083 | 1083 | 42 | 42-Clyde St/Raleigh Rd (Maribyrnong) | -37.7696986370071 | 144.898841257316 |
| 4 | 11285 | 940 | 43 | 43-Egerton Rd/Wattletree Rd (Armadale) | -37.8615917660895 | 145.02270030676 |
| 5 | 1185 | 1185 | 50 | 50-Vincent St/Wattletree Rd (Malvern East) | -37.8643850684538 | 145.04371198053 |
+----+---------+--------------+---------+--------------------------------------------+-------------------+------------------+
5 rows in set (0.06 sec)
You have renamed it to stops1. You need to use the alias instead of the original table name:
UPDATE stops s
SET real_stop_id = (SELECT MIN(sb2.stop_id)
FROM served_by sb1 JOIN
served_by sb2
USING (route_short_name)
WHERE sb1.stop_id = s.stop_id
);
I find that table abbreviations are easier to write and read.
I have 2 tables in my database. Both of have approx 100M records.My first table uph contains order's details and another urs contains customer's details. Their structure is:
mysql> desc uph;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| uid | int(11) | NO | | NULL | |
| order_from | varchar(255) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> desc usr;
+---------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| profile | char(10) | NO | | NULL | |
+---------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
Both table have data like this:
mysql> select * from usr;
+-----+----------+
| uid | profile |
+-----+----------+
| 1 | in-store |
| 2 | ecom |
| 3 | ecom |
| 4 | in-store |
| 5 | ecom |
+-----+----------+
4 rows in set (0.00 sec)
mysql> select * from uph;
+----+-----+------------+
| id | uid | order_from |
+----+-----+------------+
| 1 | 1 | in-store |
| 2 | 2 | ecom |
| 3 | 1 | ecom |
| 4 | 4 | in-store |
+----+-----+------------+
4 rows in set (0.00 sec)
Now, I want to find those users which have profile "ecom" and if they've done any purchase then order_from should only be "ecom". If not purchsed anything only having profile, still will be considered as an "ecom" user.
If any user purchased from ecom and In-store both, those will be excuded from results. Which means users should not have any relation with In-store.
So in the output of the query we'll have the result like:
+----+
| uid |
+-----+
| 2 |
| 3 |
| 5 |
+-----+
As both tables contains heavy data so I'm restricted to user sub-query. Please suggest how to do it without using sub-query.
You can do a join and check the aggregated results for your criteria
select u.uid, u.profile
from usr u
left join uph p on u.uid = p.uid
where u.profile = 'ecom'
group by u.uid, u.profile
having sum(case when p.order_from = 'in-store' then 1 else 0 end) = 0
how can i get the name who's age is max in his grade
Table information:
mysql> select * from stu;
+----+------+------+-------+
| id | name | age | grade |
+----+------+------+-------+
| 1 | a | 11 | 1 |
| 2 | b | 12 | 1 |
| 3 | c | 13 | 1 |
| 4 | d | 11 | 2 |
| 5 | e | 12 | 2 |
| 6 | f | 13 | 2 |
+----+------+------+-------+
6 rows in set (0.00 sec)
mysql> describe stu;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(1) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| grade | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
in this way i can get the max age and grade,but how can i get the name who's age is max in his grade.
mysql> select max(age),grade from stu group by grade;
+----------+-------+
| max(age) | grade |
+----------+-------+
| 13 | 1 |
| 13 | 2 |
+----------+-------+
2 rows in set (0.00 sec)
You could try this:
SELECT A.*
FROM STU A
INNER JOIN (SELECT MAX(AGE) AS MAX_AGE,GRADE FROM STU GROUP BY GRADE ) B ON A.GRADE = B.GRADE AND A.AGE= B.MAX_AGE
Try this:
select max(name), max(age),grade from stu
group by grade
If I have multiple objects and anyone can potentially map to multiple other ones.
A -> B, C, D, E
B -> A, E, F, C
C -> B
etc.
How would I store these entities in MySql and use JPA + Hibernate to manage them. For example, If I search "A", it will return "B,C,D,E", and if I search "C", it will return "B", etc.
Edited:
Follow suggestion, I did the following:
[test]> describe relation;
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_start | int(11) | YES | | NULL | |
| id_end | int(11) | YES | MUL | NULL | |
+----------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
MariaDB [test]> describe entity;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(5) | YES | | NULL | |
+-------+------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
MariaDB [test]> select * from entity;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
| 6 | F |
+----+------+
6 rows in set (0.00 sec)
MariaDB [test]> select * from relation;
+----+----------+--------+
| id | id_start | id_end |
+----+----------+--------+
| 1 | 3 | 1 |
| 2 | 2 | 1 |
| 3 | 4 | 1 |
| 4 | 5 | 1 |
| 5 | 1 | 2 |
| 6 | 5 | 2 |
| 7 | 6 | 2 |
| 8 | 3 | 2 |
| 9 | 2 | 3 |
+----+----------+--------+
9 rows in set (0.00 sec)
[test]> select E.name, R.id_start from relation R join entity E on E.id=R.id_end where E.name="A";
+------+----------+
| name | id_start |
+------+----------+
| A | 3 |
| A | 2 |
| A | 4 |
| A | 5 |
+------+----------+
Question:
This query returns the related Ids of 'A', which is right, but not actually B, C, D, E. How to get the actual related names from entity table? Is it a database design or SQL statement mistake?
I am attempting to write a migration script (between 2 versions of a program) to populate the phppos_permissions_actions table.
The rule for populating is: "If the user has permission for the module (based on phppos_permissions), then they are granted all action permissions for that module. (Which can be looked up in phppos_module_actions)".
I am trying to write a query or a set of queries that makes the following rule happen. Could someone guide me in the right direction? Below is my schema
mysql> describe phppos_modules;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| name_lang_key | varchar(255) | NO | UNI | NULL | |
| desc_lang_key | varchar(255) | NO | UNI | NULL | |
| sort | int(10) | NO | | NULL | |
| module_id | varchar(255) | NO | PRI | NULL | |
+---------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> select * from phppos_modules;
+-------------------+------------------------+------+------------+
| name_lang_key | desc_lang_key | sort | module_id |
+-------------------+------------------------+------+------------+
| module_config | module_config_desc | 100 | config |
| module_customers | module_customers_desc | 10 | customers |
| module_employees | module_employees_desc | 80 | employees |
| module_giftcards | module_giftcards_desc | 90 | giftcards |
| module_item_kits | module_item_kits_desc | 30 | item_kits |
| module_items | module_items_desc | 20 | items |
| module_receivings | module_receivings_desc | 60 | receivings |
| module_reports | module_reports_desc | 50 | reports |
| module_sales | module_sales_desc | 70 | sales |
| module_suppliers | module_suppliers_desc | 40 | suppliers |
+-------------------+------------------------+------+------------+
10 rows in set (0.00 sec)
mysql> describe phppos_modules_actions;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| action_id | varchar(255) | NO | PRI | NULL | |
| module_id | varchar(255) | NO | PRI | NULL | |
| action_name_key | varchar(255) | NO | | NULL | |
| sort | int(11) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
mysql> select * from phppos_modules_actions;
+----------------+-----------+--------------------------------+------+
| action_id | module_id | action_name_key | sort |
+----------------+-----------+--------------------------------+------+
| add_update | customers | module_action_add_update | 1 |
| add_update | employees | module_action_add_update | 130 |
| add_update | item_kits | module_action_add_update | 70 |
| add_update | items | module_action_add_update | 40 |
| add_update | suppliers | module_action_add_update | 100 |
| delete | customers | module_action_delete | 20 |
| delete | employees | module_action_delete | 140 |
| delete | item_kits | module_action_delete | 80 |
| delete | items | module_action_delete | 50 |
| delete | suppliers | module_action_delete | 110 |
| search | customers | module_action_search_customers | 30 |
| search | employees | module_action_search_employees | 150 |
| search | item_kits | module_action_search_item_kits | 90 |
| search | items | module_action_search_items | 60 |
| search | suppliers | module_action_search_suppliers | 120 |
| see_cost_price | items | module_see_cost_price | 61 |
+----------------+-----------+--------------------------------+------+
16 rows in set (0.00 sec)
mysql> describe phppos_permissions
-> ;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| module_id | varchar(255) | NO | PRI | NULL | |
| person_id | int(10) | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from phppos_permissions;
+------------+-----------+
| module_id | person_id |
+------------+-----------+
| config | 1 |
| customers | 1 |
| employees | 1 |
| giftcards | 1 |
| item_kits | 1 |
| items | 1 |
| receivings | 1 |
| reports | 1 |
| sales | 1 |
| suppliers | 1 |
| sales | 301 |
| sales | 741 |
| config | 759 |
| customers | 759 |
| employees | 759 |
| giftcards | 759 |
| item_kits | 759 |
| items | 759 |
| receivings | 759 |
| reports | 759 |
| sales | 759 |
| suppliers | 759 |
| sales | 776 |
+------------+-----------+
23 rows in set (0.00 sec)
mysql> describe phppos_permissions_actions;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| module_id | varchar(255) | NO | PRI | NULL | |
| person_id | int(11) | NO | PRI | NULL | |
| action_id | varchar(255) | NO | PRI | NULL | |
+-----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
Does
insert phppos_permissions_actions (module_id, person_id, action_id)
select distinct
phppos_permissions.module_id, phppos_permissions.person_id, action_id
from phppos_permissions
inner join phppos_modules_actions on phppos_permissions.module_id = phppos_modules_actions.module_id
order by module_id, person_id
solve your problem?