I have two tables one for units and one for Amenities
Table units
+---------+---------------------+---------------------+-----------+----------+
| unit_id | date added | date modified | unit name | user |
+---------+---------------------+---------------------+-----------+----------+
| 1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 1 | Smith |
| 2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 2 | Smith |
| 3 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 3 | Jones |
| 4 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Apartment | Smith |
+---------+---------------------+---------------------+-----------+----------+
Table Amenities
+---------+-----------+-------------------+
| id | Unit_id | Amenity |
+---------+-----------+-------------------+
| 1 | 1 | Air conditions |
| 2 | 1 | Internet |
| 3 | 1 | Water heaters |
| 4 | 1 | TV |
| 5 | 2 | TV |
| 6 | 2 | pool |
| 7 | 2 | Internet |
| 8 | 3 | Internet |
| 9 | 4 | Internet |
+---------+-----------+-------------------+
i want to select units where it has both TV and Internet
I try
select units.* from units left join Amenities on units.unit_id=Amenities.Unit_id
where Amenities.Amenity='TV' and Amenities.Amenity='Internet'
but not work
DROP TABLE IF EXISTS amenities;
CREATE TABLE amenities
(unit_id INT NOT NULL
,amenity VARCHAR(50) NOT NULL
,PRIMARY KEY(unit_id,amenity)
);
INSERT INTO amenities VALUES
(1,'Air conditions'),
(1,'Internet'),
(1,'Water heaters'),
(1,'TV'),
(2,'TV'),
(2,'pool'),
(2,'Internet'),
(3,'Internet'),
(4,'Internet');
SELECT unit_id
FROM amenities
WHERE amenity IN ('TV','Internet')
GROUP
BY unit_id
HAVING COUNT(*) = 2;
+---------+
| unit_id |
+---------+
| 1 |
| 2 |
+---------+
Obviously, you'd never have a table like the one you describe. Instead, you'd have a table of unit, a table of amenities, and a table which says which amenity belongs to which unit.
SELECT ua.*
FROM units AS ua
INNER JOIN units AS ub ON ua.unit_id = ub.unit_id
INNER JOIN Amenities AS aa
ON ua.unit_id = aa.Unit_id
AND aa.Amenity = 'TV'
INNER JOIN Amenities AS ab
ON ub.unit_id = ab.Unit_id
AND ab.Amenity = 'Internet';
Here's the output generated with your data:
+---------+---------------------+---------------------+-----------+-------+
| unit_id | date added | date modified | unit name | user |
+---------+---------------------+---------------------+-----------+-------+
| 1 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 1 | Smith |
| 2 | 2001-10-29 13:11:00 | 2001-10-29 13:11:00 | Villa 2 | Smith |
+---------+---------------------+---------------------+-----------+-------+
Related
anyone know how to grouping a record with same group but the next row the group name is blank with PHP/MySQL, for example :
Record on the database table :
| id | group_title | item | qty | price
| 1 | G-01 | Mouse | 1 | $5
| 2 | G-01 | Keyboard | 1 | $12
| 3 | G-01 | Keyboard | 1 | $12
| 4 | G-01 | Cable | 1 | $1
| 5 | G-02 | Headset | 1 | $20
And expected result :
| Group | Item Name | Qty | Price/qty | Price Total/item | Price Total/group
| G-01 | Mouse | 1 | $5 | $5 | $30
| | Keyboard | 2 | $12 | $24 |
| | Cable | 1 | $1 | $1 |
| G-02 | Headset | 1 | $20 | $20 | $20
I have tried to do it for many days, but still not solved.
Thank in advance.
You can do it with a simple IF and a uservar like this query:
SELECT
IF(g.gname = #myg, '', #myg:= gname) AS gname,
g.mytext
FROM grouptab g
CROSS JOIN ( SELECT #myg:='') AS init
ORDER BY g.gname;
Sample
MariaDB [test]> select * from grouptab;
+----+-------+---------+
| id | gname | mytext |
+----+-------+---------+
| 1 | G01 | Name 1 |
| 2 | G01 | Name 2 |
| 3 | G02 | Name 88 |
| 4 | G02 | Name 99 |
| 5 | G01 | Name 4 |
+----+-------+---------+
5 rows in set (0.000 sec)
MariaDB [test]> SELECT
-> IF(g.gname = #myg, '', #myg:= gname) AS gname,
-> g.mytext
-> FROM grouptab g
-> CROSS JOIN ( SELECT #myg:='') AS init
-> ORDER BY g.gname;
+-------+---------+
| gname | mytext |
+-------+---------+
| G01 | Name 1 |
| | Name 2 |
| | Name 4 |
| G02 | Name 88 |
| | Name 99 |
+-------+---------+
5 rows in set (0.000 sec)
MariaDB [test]>
invoice
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
I want to select all rows except the duplicate BRN. (If there are two/more ge in BRN then it should only select one)
I tried:
SELECT *(DISTINCT BRN) FROM invoice
Expected result:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
Given the following table:
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
with the expected results:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
The difficult part is getting the amount, because it is arbitrary, not to mention that the values in Amnt are pretty much worthless in this result.
If you want distinct BRN, the query would be SELECT DISTINCT BRN FROM invoice
You might even get away with SELECT DISTINCT BRN, Name FROM invoice
An intermediate step would be SELECT BRN,Name FROM invoice GROUP BY BRN, Name
But if you try to include Amnt in the equation, then the query will fail because there's no way for the database to determine which Amnt to show.
So, you could try this kludge:
SELECT a.BRN, a.Name, b.Amnt FROM invoice AS a LEFT JOIN invoice AS b ON a.BRN=b.BRN
No guarantees on which Amnt it will pick up, though.
Hope that helps.
SELECT * FROM invoice WHERE Date >= :fdate GROUP BY BRN
See Here Use GROUP BY in Query with your Conditions
In this case each person can be assigned to an unlimited number of companies. One of these assigned companies should always be the person's main company. As you can see in the SQL query which I have posted the ID of the person's main organization is stored in the column "main_company_id" in the "person" table.
I have database Internal on server 192.168.0.1 with Person_Company Table as given
+----+-----------+------------+------------+
| id | person_id | company_id | created_at |
+----+-----------+------------+------------+
| 1 | 1005 | 2589 | 00:00:00 |
| 2 | 1006 | 2590 | 00:00:00 |
| 3 | 1007 | 2591 | 00:00:00 |
+----+-----------+------------+------------+
Person Table is as given
+------+-------+---------+-----------------+-----------------+
| id | name | phone | main_company_id | ref_id |
+------+-------+---------+-----------------+-----------------+
| 1005 | John | 0123456 | 2590 | 273722702297768 |
| 1006 | Doe | 7894560 | 2591 | 955413080598021 |
| 1007 | Smith | 9517530 | 2589 | 164283934074454 |
+------+-------+---------+-----------------+-----------------+
Company Table is as given
+------+-----------+---------+-----------------+
| id | name | vat | ref_id |
+------+-----------+---------+-----------------+
| 2589 | Company A | 0123456 | 540603005841231 |
| 2590 | Company B | 7894560 | 725472422399397 |
| 2591 | Company C | 9517530 | 367043795528136 |
+------+-----------+---------+-----------------+
Now there is another database External on the same server 192.168.0.1 with External_Person tables as given:
+----+-----------------+-------+----------------------+--------+
| id | ref_id | name | internal_primary_key | gender |
+----+-----------------+-------+----------------------+--------+
| 1 | 273722702297768 | John | ABC123456 | male |
| 2 | 955413080598021 | Doe | BCD456789 | female |
| 3 | 164283934074454 | Smith | DEF789456 | male |
+----+-----------------+-------+----------------------+--------+
And another table on this External database is External_Company
+----+-----------------+-----------+----------------------+
| id | ref_id | name | internal_primary_key |
+----+-----------------+-----------+----------------------+
| 1 | 540603005841231 | Company A | XX4123456 |
| 2 | 725472422399397 | Company B | XX5456789 |
| 3 | 367043795528136 | Company C | XX6789456 |
+----+-----------------+-----------+----------------------+
What I want to achieve is like this result:
+----+------------+-------------+------------------+
| id | person_key | company_key | main_company_key |
+----+------------+-------------+------------------+
| 1 | ABC123456 | XX4123456 | XX5456789 |
| 2 | BCD456789 | XX5456789 | XX6789456 |
| 3 | DEF789456 | XX6789456 | XX4123456 |
+----+------------+-------------+------------------+
I have already achieved two columns through this statement:
SELECT EEP.internal_primary_key as Person_Key, EEC.internal_primary_key as Company_Key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
WHERE IPC.person_id = 1005;
And this has the output like this result:
+----+------------+-------------+
| id | person_key | company_key |
+----+------------+-------------+
| 1 | ABC123456 | XX4123456 |
| 2 | BCD456789 | XX5456789 |
| 3 | DEF789456 | XX6789456 |
+----+------------+-------------+
How can I get the main company's internal_primary_key of the person in this given scenario? How can I amend my this existing query to achieve the desired result which I have mentioned above?
Does this work for you? You have a lot of good work, it seems all you would need is add the EEC.internal_primary_key as main_company_key code to your select statement.
SELECT EEP.internal_primary_key as person_Key, EEC.internal_primary_key as company_Key, EEC.internal_primary_key as main_company_key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
WHERE IPC.person_id = 1005;
I have done this successfully after doing some research upon databases
SELECT EEP.internal_primary_key as Person_Key, EEC.internal_primary_key as Company_Key, MEEC.internal_primary_key as Main_Company_Key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
JOIN Internal.Company as MIC on MIC.id = IP.main_company_id
JOIN External.External_Company as MEEC on MIC.ref_id = MEEC.ref_id
WHERE IPC.person_id = 1005;
And now this results as I wanted:
+----+------------+-------------+------------------+
| id | person_key | company_key | main_company_key |
+----+------------+-------------+------------------+
| 1 | ABC123456 | XX4123456 | XX5456789 |
| 2 | BCD456789 | XX5456789 | XX6789456 |
| 3 | DEF789456 | XX6789456 | XX4123456 |
+----+------------+-------------+------------------+
I want to treat data from mysql for display in HTML, using PHP.
I have three database tables: student, course, student_x_course
student:
| idStudent | firstname | surname |
-----------------------------------
| 1 | John | Regular |
| 2 | John | Smith |
| 3 | Claire | White |
course:
| idCourse | coursename |
--------------------------
| 1 | Art |
| 2 | Music |
| 3 | Math |
| 3 | Biology |
student_x_course:
| idsc | idStudent | idCourse |
-------------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 2 | 4 |
| 7 | 3 | 1 |
| 8 | 3 | 2 |
| 9 | 3 | 3 |
| 10 | 3 | 4 |
And I want to create an html table which looks like this:
| Art | Music | Math | Biology |
------------------------------------------------
John Regular | x | x | x | - |
John Smith | x | x | - | x |
Claire White | x | x | x | x |
My sql query is:
SELECT s.firstname, s.surname, c.coursename FROM student AS s INNER JOIN student_x_course AS sxc ON (s.idStudent = sxc.idStudent) INNER JOIN course ON (c.idCourse = sxc.idCourse);
which gets me the following:
| John | Regular | Art |
| John | Regular | Music |
| John | Regular | Math |
| John | Smith | Art |
| John | Regular | Music |
| John | Smith | Biology |
| Claire | White | Art |
| Claire | White | Music |
| Claire | White | Math |
| Claire | White | Biology |
My question is: How can I get from many rows to lines?
Is there a better sql query or do I have to handle this in PHP Code?
Any suggestions?
You are trying to pivot your results. In mysql, you can do this with conditional aggregation:
SELECT s.idStudent, s.firstname, s.surname,
max(case when c.coursename = 'Art' then 'x' end) Art,
max(case when c.coursename = 'Music' then 'x' end) Music,
max(case when c.coursename = 'Math' then 'x' end) Math,
max(case when c.coursename = 'Biologoy' then 'x' end) Biologoy
FROM student AS s
INNER JOIN student_x_course AS sxc ON (s.idStudent = sxc.idStudent)
INNER JOIN course ON (c.idCourse = sxc.idCourse)
GROUP BY s.idStudent
I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;