I have two simple tables in a database.
The schema looks like this:
I have created an SQL-Fiddle with some example-data.
Link: http://sqlfiddle.com/#!9/c5e87c/2
At first I want to simple query for all Printers that should get connected to a Computer.
I select the table computermapping and query for a ComputerGUID like this:
SELECT PrinterGUID
FROM computermapping
WHERE ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
This is OK. At second, I want to join the table "computerdefaultprinter".
I use a LEFT JOIN on the ComputerGUID like this:
SELECT computermapping.PrinterGUID
FROM computermapping
LEFT JOIN computerdefaultprinter ON computerdefaultprinter.ComputerGUID = computermapping.ComputerGUID
WHERE computermapping.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
Also OK.
Finally I want to know, which Printer will be the Default-Printer. A Computer can only have one Default-Printer. Using my sample data in the SQL-Fiddle I want the output to be like this:
PrinterGUID | isDefaultPrinter
--------------------------------------------------------
5549f63f-e02f-4685-a976-96b50c299bed | 1
957b7233-e590-4e7d-aed6-aee0573fc3a8 | 0
5106f1f7-068f-463f-9b76-7cc0ba017184 | 0
I used something in the past like:
SELECT computermapping.PrinterGUID, ( computerdefaultprinter.PrinterGUID IS NOT NULL) as isDefaultPrinter
...
But I can't get it to work anymore. I haven't used SQL for some years now.
Currently I am using MySQL, if this is important.
Could you please help me to solve this?
Thank you.
PS: I had problems to find an adequate title for my request. I don't know if using "computed columns" mentioned in the title is correct.
You are missing the condition that relates the PrinterGUIDs of the 2 tables in the ON clause:
SELECT m.PrinterGUID,
p.PrinterGUID IS NOT NULL AS isDefaultPrinter
FROM computermapping AS m LEFT JOIN computerdefaultprinter AS p
ON p.ComputerGUID = m.ComputerGUID AND p.PrinterGUID = m.PrinterGUID
WHERE m.ComputerGUID = '5bec3779-b002-46ba-97c4-19158c13001f'
See the demo.
Results:
> PrinterGUID | isDefaultPrinter
> :----------------------------------- | ---------------:
> 5549f63f-e02f-4685-a976-96b50c299bed | 1
> 957b7233-e590-4e7d-aed6-aee0573fc3a8 | 0
> 5106f1f7-068f-463f-9b76-7cc0ba017184 | 0
Related
I have following query:
http://www.sqlfiddle.com/#!9/752e34/3
This query use SELECT in SELECT queries.
"SELECT a.*
,(SELECT s.value FROM tbl_scd AS s WHERE s.tag_id = 1 AND s.main_id = a.id ORDER BY s.date_time DESC LIMIT 1) AS title
,(SELECT s.value FROM tbl_scd AS s WHERE s.tag_id = 2 AND s.main_id = a.id ORDER BY s.date_time DESC LIMIT 1) AS alt
FROM tbl_main AS a
WHERE 1;"
Now I'm looking for a solution to add a new row into tbl_tag without change the above query (that the SELECT in SELECT part will be dynamic) to get a reference to tbl_tag
To get this:
+----+---------------+-----------+-----------+--------------+
| id | date | title | alt | new_column |
+----+---------------+-----------+-----------+--------------+
| 1 | 2018-10-10 | test1-1 | test1-3 | NULL |
| 2 | 2018-10-11 | test2-1 | test2-1 | NULL |
+----+---------------+-----------+-----------+--------------+
It would be great to get an idea or help.
Thanks
Your last comment on your question about using JOIN makes it clearer to me (I think) what you are after. JOINs will definitely help you a lot here, in place of the rather cumbersome query you are currently using.
Try this:
SELECT
tbl_main.date,
tblA.value AS title,
tblB.value AS alt
FROM
tbl_main
INNER JOIN (SELECT main_id, tag_id, value
FROM tbl_scd
INNER JOIN tbl_tag ON (tbl_scd.tag_id = tbl_tag.id)
WHERE tbl_tag.name = 'title') tblA
ON (tbl_main.id = tblA.main_id)
INNER JOIN (SELECT main_id, tag_id, value
FROM tbl_scd
INNER JOIN tbl_tag ON (tbl_scd.tag_id = tbl_tag.id)
WHERE tbl_tag.name = 'alt') tblB
ON (tbl_main.id = tblB.main_id);
I think this will get you much closer to a general solution to what it looks like you are trying to achieve, or at least point you in a good direction with using JOINs.
I also think you might benefit from re-thinking your database design, because this kind of pivoting rows from one table into columns in a query output can be an indicator that the data might be better off structured differently.
In any case, I hope this helps.
I would like to do some new stuff (for me it's new, bc. I'm just a MySQL-beginner) and I did not find a solution for this.
I got these entries in my database:
mytable_items
id | title | catids
1 | test | 32,14
mytable_categories
id | title
32 | Test-Category
14 | Another-Category
Now I would like to join this stuff: Show all data from mytable_items - also show the assigned categories (their titles)
The result should be:
1 | test | Test-Category, Another-Category
How can I solve this?
Thanks a lot in advance :-)
Try this:
SELECT m.id,group_concat(mc.title)
FROM mytable_items m
JOIN mytable_categories mc
ON FIND_IN_SET(mc.id,m.catids)
group by
m.id
SQL FIDDLE DEMO
You should use different entities for each CatID. Then you can join both tables and use Group Concat.
Try this:
Select *
from mytable_items a
join mytable_categories b on a.id = b.id
This will join the data and show it correctly.
I have two tables with the following structures:
exp_hotel
hot_id | hot_webid | hot_name | hot_starrating | hot_brandbool | hot_latitude | hot_longitude
exp_result
res_id | res_idHotel | res_rank | res_price | res_userRating | res_oldPrice | res_posa | res_date | res_date | res_que_id
res_posa has sometimes either the value 'xxx' or the value 'yyy'.
If it has the value 'xxx', the SELECT should be something like:
SELECT * FROM exp_hotel JOIN exp_result ON hot_id = res_idHotel
And if res_posa value is 'yyy' than the SELECT would be:
SELECT * FROM exp_hotel JOIN exp_result ON hot_webid = res_idHotel
Is there a way how to create a query wich would select everything with the correct JOIN structure directly from MySQL without going through some PHP arrays and so on?
You can use conditional join like below.
SELECT * FROM exp_hotel JOIN exp_result ON
(res_posa = 'XXX' and hot_id = res_idHotel)
OR
(res_posa = 'YYY' and hot_webid = res_idHotel);
Mock up fiddle here.
I have two tables reports & viewedids
the first one stores data and the second one stores user IDs that viewed data:
viewedids:
+-------+------+
| uid | rid |
+-------+------+
| 2 | 5 |
+-------+------+
each (uid,rid) means the uid has viewd rid
I want to select * from reports table and add view state (0 or 1) for current user to it. (A JOIN statement)
How can I do this?
You can use a LEFT JOIN:
SELECT reports.*, viewedids.uid IS NOT NULL as view_state
FROM
reports LEFT JOIN viewedids
ON reports.id = viewedids.rid
AND viewedids.uid = #current_user
This will return all reports, and will try to join reports table with viewedids ON reports.id = viewedids.rid AND viewedids.uid = #current_user. If the join succedes, viewedids.uid will be not null, and viewedids.uid IS NOT NULL will be evaluated to 1. It will be evaluated 0 otherwise.
Please see fiddle here.
I try to combine data from 2 tables by a mysql join but I look likes, I don't receive the results from the second table at all.
Table structre #1 (site_hosters);
+------------+--------------+--------+
| host_id | name | prio |
+------------+--------------+--------+
| 1 | site.com | 0 |
+------------+--------------+--------+
Table structure #2 (site_date);
+------------+--------------+--------+
| id | hoster | page |
+------------+--------------+--------+
| 1 | site.com | http:..|
+------------+--------------+--------+
What I try to get is a result like 'id, host_id, name, ....etc';
When I try the follow query, it doesn't take host_id and prio in the query result.
It looks, the join has no effect at all by query it.
My query:
SELECT
site.id,
site.hoster,
site.page,
FROM site_data as site
INNER JOIN site_hosters hoster
ON site.hoster = hoster.name
I hope someone can help me with this one.
Kind regards,
Nick
You have to name the columns you want to select. Add the site_hosters columns like that:
SELECT
site.id,
site.hoster,
site.page,
hoster.host_id,
hoster.prio
FROM site_data as site
INNER JOIN site_hosters hoster ON site.hoster = hoster.name
You could do
SELECT
site.*,
hoster.*
FROM site_data as site
INNER JOIN site_hosters hoster ON site.hoster = hoster.name
This will return all fields, however, you may only want something like
site.id, ste.hoster, site.page, hoster.prio as your fields.
Just list required fields in SELECT:
SELECT site.id, hoster.host_id, hoster.name, site.hoster, site.page
FROM site_data AS site
INNER JOIN site_hosters AS hoster ON site.hoster = hoster.name