hibernate join table mysql - mysql

I have two simple tables.
mysql> select * from filesshare;
+----+--------+--------+-------+
| id | userId | fileId | owner |
+----+--------+--------+-------+
| 1 | 2 | 1 | 1 |
| 2 | 3 | 1 | 1 |
| 3 | 4 | 2 | 1 |
| 4 | 5 | 2 | 1 |
+----+--------+--------+-------+
4 rows in set (0.00 sec)
mysql> select * from filesinfo;
+----+-----------------+-----------------------------------+
| id | name | url |
+----+-----------------+-----------------------------------+
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
+----+-----------------+-----------------------------------+
2 rows in set (0.01 sec)
i will have now two values as input. 1) userId 2) owner
now assume userId=3 and owner=1. Now i want out put to match all the rows with given values in filesshare table in this case its second row that is " 2 | 3 | 1 | 1 " now i want that column three value which is 1 now. Then with that value i want to retrieve the data from filesinfo table. in this case the first row of filesinfo table will b the output. like this:
+----+-----------------+-----------------------------------+
| id | name | url |
+----+-----------------+-----------------------------------+
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
+----+-----------------+-----------------------------------+
is that possible using joins using hibernate, a simple example can be a kickstart for me.
i tried this
mysql> Select fileid,name,url from filesshare fs,filesinfo fi where fs.fileid=fi
.id;
+--------+-----------------+-----------------------------------+
| fileid | name | url |
+--------+-----------------+-----------------------------------+
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 1 | dwnld_btn.png | C:\shareapp\admin\dwnld_btn.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
+--------+-----------------+-----------------------------------+
Thanks and regards

in hibernate you can deal with it two ways:
use plain sql
example:
SQLQuery query = session.createSQLQuery("select fi.* from fileshare fs, filesinfo fi where fs.fileId =fi.id and fs. userId = :uid and fs.owner = :oid");
query.setInteger("uid", userid);
query.setInteger("oid", ownerid);
/*maybe deal with transformation */
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);)
return (List<Map<String,Object>>)query.list();
here is the link with info on transformation details:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-associations
use hql assuming you have entities mapped to
example:
Criteria crit = session.createCriteria(Filesinfo.class)
crit.setFetchMode("fs.fileshare", FetchMode.JOIN);
crit.add( Restrictions.eq("fs.userId", userid) Restrictions.eq("fs.owner", ownerid) );
return crit.list();
all info on this method is here: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html
and here is the details of teh ORM mapping via xml: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/xml.html

Select id,name,url from fileshare fs,filesinfo fi where fs.id=fs.id;

k guys this one one after scratching head, of course help from google also.
mysql> select a.id, a.name, a.url from filesinfo a inner join filesshare b on
a.id=b.fileid where b.userid=5 and b.owner=1;
+----+-----------------+-----------------------------------+
| id | name | url |
+----+-----------------+-----------------------------------+
| 2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
| 4 | loader3.gif | C:\shareapp\admin\loader3.gif |
+----+-----------------+-----------------------------------+
2 rows in set (0.02 sec)

Related

How to access a row on a table based on a value from another table?

I am doing a mini project using MySQL. I came with the following problem:
I created 2 tables, student and book with 6 and 5 columns respectively.
mysql> select * from book;
+--------+------+------------+---------+------+
| bookid | Name | Authorname | edition | cost |
+--------+------+------------+---------+------+
| cc12 | dbms | guna | 5 | 500 |
| cc34 | CA | fasil | 5 | 600 |
| cs113 | OS | rohan | 3 | 300 |
| cs12 | AI | ganesh | 2 | 1000 |
| cs343 | c# | jackesh | 4 | 300 |
+--------+------+------------+---------+------+
5 rows in set (0.00 sec)
mysql> select * from studentbook;
+-----+--------+-----------+
| Sno | bookid | Studid |
+-----+--------+-----------+
| 1 | cc12 | 14vec1088 |
| 2 | cs113 | 14vec1099 |
| 3 | cc34 | 14vec1132 |
| 4 | cs343 | 14vec2011 |
| 5 | cs12 | 14vec100 |
+-----+--------+-----------+
5 rows in set (0.00 sec)
Now, when I enter any of the studid mentioned in the table studentbook (This is performed by PHP in the backend) it should display the details of book associated with the respective studid from the table book.
How can I perform the above using MySQL Query?
This might be work.
SELECT * FROM book
WHERE bookid IN
(SELECT bookid FROM studentbook
WHERE studid = "[Id of which you want book]");
This should get you what you need.
SELECT b.*
FROM book b
INNER JOIN studentbook sb on b.bookid = sb.bookid
WHERE sb.Studid = [your id]

Joining 3 tables from two different Databases?

Database 1 (2 tables) : sandbox_maesc4
table 1: coordinates
+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
| 1 | A | A7 |
| 2 | B | B20 |
| 3 | C | C3 |
| 4 | D | D14 |
| 5 | E | E9 |
+----------------------------------------+
table 2: workstation_userlogged
+----------------------------------+
| id | ws | user |
+----------------------------------+
| 1 | COMP123 | ryan |
| 2 | COMP345 | luda |
| 3 | COMP567 | chris |
| 4 | COMP891 | michel|
| 5 | COMP444 | isabel|
+----------------------------------+
Database 2 (1 table): softphone_materials_updater
Table 1: workstation
+----------------------------+
| ID | ws | pod |
+----------------------------+
| 1 | COMP123 | A07 |
| 2 | COMP345 | B20 |
| 3 | COMP567 | C03 |
| 4 | COMP891 | D14 |
| 5 | COMP444 | E09 |
+----------------------------+
Problem:
I only have read access in Database 2.
So I did a SELECT query for both fields and created the table "userlogged".
Now, I want to combine both tables "coordinates" and "userlogged" by joining table "workstation"
with their relation of the "station_number" field and "pod" field from. How can I achieve this? Below is the query that I tried but doesnt work.
I have extra fields in "coordinates" table (X,Y fields with actual coordinates). In PHP I use all fields to show them on screen.
SELECT
coordinate_id,
section_name,
x_coord,
y_coord,
ws.username,
ws.hostname,
w.pod,
FROM
sandbox_maesc4.coordinates c,
sandbox_maesc4.workstation_userlogged ws
INNER JOIN
softphone_materials_updater.workstations w
ON c.station_number = w.pod
I think maybe this is what you want?
SELECT
coordinate_id,
section_name,
x_coord,
y_coord,
wsu.username,
wsu.hostname,
w.pod
FROM
sandbox_maesc4.coordinates c
INNER JOIN
softphone_materials_updater.workstations w
ON c.station_number = w.pod
INNER JOIN
sandbox_maesc4.workstation_userlogged wsu
ON w.ws = wsu.ws
Not sure about the database and table names, they seem to differ between your sample query and the description.

COUNT reducing results?

A query without COUNT returns 3 records, with only 1.
SELECT `blog_cate` . * , COUNT( blogi.blog_cate ) AS num
FROM (
`blog_cate`
)
JOIN `blogi` ON `blogi`.`blog_cate` = `blog_cate`.`blogi_cate_url`
results:
+----+------------------+----------------+-----+
| id | blogi_cate_title | blogi_cate_url | num |
+----+------------------+----------------+-----+
| 1 | Базы данных | batabase | 3 |
+----+------------------+----------------+-----+
And the same query, but without a COUNT:
SELECT `blog_cate` . *
FROM (
`blog_cate`
)
JOIN `blogi` ON `blogi`.`blog_cate` = `blog_cate`.`blogi_cate_url`
That returns me 3 records:
+----+------------------+----------------+
| id | blogi_cate_title | blogi_cate_url |
+----+------------------+----------------+
| 1 | Базы данных | batabase |
| 1 | Базы данных | batabase |
| 3 | Разработка | razrabotka |
+----+------------------+----------------+
Is it possible to use a COUNT and have a normal results?
p.s. tables:
+----+------------+
| id | blog_cate |
+----+------------+
| 1 | batabase |
| 2 | batabase |
| 3 | razrabotka |
+----+------------+
+----+------------------+----------------+
| id | blogi_cate_title | blogi_cate_url |
+----+------------------+----------------+
| 1 | Базы данных | batabase |
| 2 | PHP | php |
| 3 | Разработка | razrabotka |
+----+------------------+----------------+
COUNT() with out a group by will group all records and produce a count of them. Adding more fields to the select will only show the details of the first record
You could build one query to get the three rows and one query to get the count result and join them via cross join to combine every detail row with the count row.

SELECT all records but the highest

I have three tables.
Page Revision Text
+-----------+-------------+ +-----------+-------------+----------+ +-------------+---------+
| page_id | rev_id | | rev _id | rev_text_id | page_id | | rev_text_id | text |
+-----------+-------------+ +-----------+-------------+----------+ +-------------+---------+
| 1 | 2 | | 1 | 1 | 1 | | 1 | A |
+-------------------------- +------------------------------------+ +-----------------------+
| 2 | 5 | | 2 | 2 | 1 | | 2 | B |
+-------------------------+ +------------------------------------+ +-----------------------+
| 3 | 3 | 2 | | 3 | C |
+------------------------------------+ +-----------------------+
| 4 | 4 | 2 | | 4 | D |
+------------------------------------+ +-----------------------+
| 5 | 4 | 2 |
+------------------------------------+
My desired output is:
+--------+-------------+
| rev_id | rev_text_id |
+--------+-------------+
| 1 | 1 |
+----------------------+
| 3 | 3 |
+----------------------+
| 4 | |
+----------------------+
My goal is to find rev_id's and rev_text_id's that can be deleted from each table in a single query. As the revision table can hold equal rev_text_id's for multiple revisions, I need to exclude the rev_text_id that is highest for each page (in the example 4 for page 2).
I began dabbling along the lines of:
SELECT
r.rev_id,r.rev_text_id
FROM revision r, page p, text t
WHERE p.page_id = r.page_id
AND t.rev_text_id = r.rev_text_id
AND p.rev_id != r.rev_id
AND r.rev_text_id not in (
SELECT max(r.rev_text_id)
FROM revision AS r, page AS p
WHERE r.rev_id = p.rev_id
);
But that skips the record for rev_id 4 in my desired output and doesn't include the "null cases" ..
Try this:
SELECT r.`rev_id`, r.`rev_text_id`
FROM `Revision` AS r
WHERE r.`rev_text_id` NOT IN (SELECT r.`rev_text_id`
FROM `Revision` AS r, `Page` AS p
WHERE r.`rev_id` = p.`page_latest`;)
ORDER BY r.`rev_id`;
This basically grabs the rev_id's that equal the page_latest, finds the rev_text_id's associated with those rev_id's, and uses those rev_text_id's as the filter.

Fixing Joined Many-to-Many MySQL Query

I have two tables that look like this:
Table A:
+-----+-----+------+-------+
| aID | uID | attr | value |
+-----+-----+------+-------+
| 1 | 1 | fn | john |
+-----+-----+------+-------+
| 2 | 1 | ln | smith |
+-----+-----+------+-------+
| 3 | 2 | fn | jim |
+-----+-----+------+-------+
| 4 | 2 | ln | bean |
+-----+-----+------+-------+
Table B:
+-----+-----+-------+-------+
| bID | uID | perm | value |
+-----+-----+-------+-------+
| 1 | 1 | admin | 1 |
+-----+-----+-------+-------+
| 2 | 2 | news | 1 |
+-----+-----+-------+-------+
| 3 | 2 | cms | 1 |
+-----+-----+-------+-------+
As it shows, Table A holds attribute data for a user uID, and Table B holds permission data for a user uID.
At the moment, I am using,:
SELECT GROUP_CONCAT(`a`.`attr`) AS `attrs`
, GROUP_CONCAT(`a`.`value`) AS `values`
, GROUP_CONCAT(`b`.`perm`) AS `perms`
FROM `a`
JOIN `b`
ON `a`.`uID` = `b`.`uID`
GROUP BY `a`.`uID`, `b`.`uID`
But it is giving me a result:
+-------------+-------------------+-------------------+
| attrs | values | perms |
+-------------+-------------------+-------------------+
| fn,ln | John,Smith | admin,admin |
+-------------+-------------------+-------------------+
| fn,fn,ln,ln | Jim,Jim,Bean,Bean | news,cms,news,cms |
+-------------+-------------------+-------------------+
What do I need to change in my query to get:
+-------+------------+----------+
| attrs | values | perms |
+-------+------------+----------+
| fn,ln | John,Smith | admin |
+-------+------------+----------+
| fn,fn | Jim,Bean | news,cms |
+-------+------------+----------+
GROUP_CONCAT takes additional arguments, as explained on its documentation page here.
The one you want is distinct:
SELECT GROUP_CONCAT(distinct `a`.`attr`) AS `attrs` . . .