I have 8 different tables and I would like to select only specific columns in one query. If I use this query I have wrong results :(
SELECT a.entity_id id
, a.field_imie_value imie
, c.field_nazwisko_value nazwisko
, d.field_preferencja_1_value preferencja1
, e.field_preferencja_2_value preferencja2
, f.field_preferencja_3_value preferencja3
, g.field_nr_niu_value nr_niu
, h.dystans odleglosc
, i.field_sytuacja_value sytuacja
FROM field_data_field_imie a
JOIN field_data_field_nazwisko c
, field_data_field_preferencja_1 d
, field_data_field_preferencja_2 e
, field_data_field_preferencja_3 f
, field_data_field_nr_niu g
, field_data_field_adres h
, field_data_field_sytuacja i
WHERE a.entity_id = b.entity_id
AND a.entity_id=c.entity_id
When you use joining tables you must use syntax similar to :
select A.field1 from table1 AS A inner join table2 as B on A.field1 = B.field1
syntax should be like this;
SELECT *
FROM field_data_field_imie a
JOIN field_data_field_nazwisko c on (a.entity_id=c.entity_id)
JOIN table x on (x.column=a.entitiy_id)
or you shouldn't use join at all.
actually number of tables is the important thing not how many fields you want however this is the syntax for joining more table :
elect A.field1 from table1 AS A inner join table2 as B on A.field1 = B.field1 inner join table3 AS C on table1.field1 = C.field1
and syntax for more joining is similar
Related
I have bd hf3 and 5 tables there:
active_preset with columns (id , preset_id)
preset with columns (id , birja_id, trend_id, fractal, interval_up)
birja with columns (id , name)
trend with columns (id , name)
uq_active_preset with columns (id , birja, trend, fractal, interval_up)
In table preset I have a few records. Some of them are in table active_preset by foreign key preset_id. In table active_preset a few records exist once , a few more than once.
I need to update table uq_active_preset with records from table active_preset disregarding repetitions of records if they are present.
I did query from active_preset and it works good:
SELECT
b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
FROM hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
But I don't know how to update uq_active_preset
I tried this and it returns syntax error:1064 :
UPDATE hf3.uq_active_preset uap SET
uap.birja = st.birja ,
uap.fractal = st.fractal,
uap.trend = st.trend,
uap.interval_up = st.interval_up,
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
when you make an update using from is like you join the updated table with your query result. So, you need also a where statement in order to tell where those two are connected. Also, don't use alias of your updated table on set statement.
You need something like that:
UPDATE hf3.uq_active_preset uap SET birja=st.birja,fractal=st.fractal,trend=st.trend,interval_up=st.interval_up
FROM (SELECT b.name AS birja, p.fractal AS fractal , tre.name AS trend, p.interval_up AS interval_up
from hf3.active_preset AS ap
INNER JOIN hf3.preset AS p on p.id = ap.preset_id
INNER JOIN hf3.birja AS b on b.id = p.birja_id
INNER JOIN hf3.trend AS tre on tre.id = p.trend_id
GROUP BY b.name, p.fractal, tre.name, p.interval_up
HAVING COUNT(*) >= 1
) st
where uap.fkey=st.fkey
Usually when I need to join the row with some max value from another column I do something like:
SELECT *
FROM a
INNER JOIN b ON (a.aid = b.aid)
LEFT JOIN b nullb ON (a.aid = nullb.aid AND nullb.value > b.value)
WHERE nullb.bid IS NULL;
I'm not sure if this is the most efficent way but it is a solution without subqueries which i avoid whenever possible.
Today I needed to join in the max value from another table that joins in via a jointable and could not figure out a way to do it.
I tried something like this: (which failed miserably)
SELECT *
FROM a
INNER JOIN atob ON (a.aid = atob.aid)
INNER JOIN b ON (atob.bid = b.bid)
LEFT JOIN atob nullatob ON (a.aid = nullatob.aid)
LEFT JOIN b nullb ON (nullatob.bid = nullb.bid AND nullb.value > b.value)
WHERE nullb.bid IS NULL;
I set up an sqlfiddle at: http://sqlfiddle.com/#!9/86f18/6 with the problem set up.
Anyone got a clever way to join in the max value from another table via a jointable without using subqueries or is that pretty much impossible?
Not sure what is your goal but:
http://sqlfiddle.com/#!9/86f18/13
SELECT A.*, b.*
FROM A
INNER JOIN AtoB
ON (A.Aid = AtoB.Aid)
LEFT JOIN (
SELECT B.*
FROM B
LEFT JOIN b nullB
ON (B.Bid = nullB.Bid AND nullB.bdate > B.Bdate)
WHERE nullB.Bid IS NULL
) b
ON AtoB.Bid = b.Bid
Can anyone help me to optimize this query ? I can probably add some indexes and get only date from the modified (CURRENT_TIMESTAMP) field ?
Thanks
Jeremy
SELECT a.providerName AS Assureur, b.insuranceType AS Produit, c.votedFor, c.votedFor2, c.email, c.comment, c.modified
FROM insuranceproviders AS a, insurancetypes AS b, insurancevotes AS c
WHERE a.id = c.providerId
AND b.id = c.insTypeId
following join query looks better:
SELECT a.providerName AS Assureur, b.insuranceType AS Produit,
c.votedFor, c.votedFor2, c.email, c.comment, c.modified
FROM insuranceproviders AS a
INNER JOIN insurancetypes AS b ON a.id = c.providerID
INNER JOIN insurancevotes AS c ON b.id = c.insTypeId
and You should add following INDEX:
ALTER TABLE insuranceproviders ADD INDEX(id);
ALTER TABLE insurancetypes ADD INDEX (providerID, insTypeId);
ALTER TABLE insurancevotes ADD INDEX(insTypeId);
You can try like below:
[Here I am doing Left join as i dont know the table structure instead you can also have inner join]
SELECT a.providerName AS Assureur, b.insuranceType AS Produit, c.votedFor, c.votedFor2, c.email, c.comment, c.modified
FROM insurancevotes AS c
LEFT JOIN insuranceproviders AS a ON a.id = c.providerId
LEFT JOIN insurancetypes AS b ON b.id = c.insTypeId
use indexes for a.id, c.providerId, b.id, c.id(if there is), c.insTypeId
http://i.stack.imgur.com/mbUTI.jpg
I want to Do a multi select in one query where one table has data from many tables.
i have four tables to combine it into a single output.
here is a image of my table.
i want to select all and don't want other data from other table just main table with name of all other tables
have tried following but its not working.
select * from project_content
left Join project_master on project_master.id = project_content.p_id
left Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left Join project_menu_master on project_menu_master.id = project_content.m_id
select * from project_content
left Join project_master on project_master.id = project_content.p_id
left Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left Join project_menu_master on project_menu_master.id = project_content.m_id
select distinct(*) from project_content
left OUTER Join project_master on project_master.id = project_content.p_id
left OUTER Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left OUTER Join project_menu_master on project_menu_master.id = project_content.m_id
select * from project_content ,project_master,project_content_menu,project_menu_master
where project_master.id = project_content.p_id and project_content_menu.type_id = project_content.p_c_id and project_menu_master.id = project_content.m_id
select pc.id as id , pm.name as pname , pmm.name as menuname , pcm.name as contentname , pc.name as name
from
project_content as pc,
project_master as pm,
project_content_menu as pcm,
project_menu_master as pmm
where
pm.id = pc.p_id
and
pcm.type_id = pc.m_id
and
pmm.id = pc.p_c_id
If I undesrtood you correctly, you want the data just from the main table, but with names instead of foreign keys from other tables? If so, then:
SELECT pc.id, pm.name, pcm.name, pmm.name, pc.name, pc.desc, pc.thumb, pc.src, pc.status
FROM project_content AS pc
LEFT JOIN project_master AS pm ON pm.id = pc.p_id
LEFT JOIN project_content_menu AS pcm ON pcm.type_id = pc.p_c_id
LEFT JOIN project_menu_master AS pmm ON pmm.id = pc.m_id
Im not an expert in SQL but you could try using UNION operator. like this:
select names
from table1
where lastname like k%
UNION
select names
from table2
where lastname like k%
This will combine the result from table1 and table2 and display UNIQUE NAMES in the result where the lastname is starting with k. so, if there is a JOHN KRAMER and JOHN KUTCHER then only JOHN will be displayed once.
If you want duplicate entries , too, then use UNION ALL
I'm not sure, that I understood your problem well, but if you need to add all records together, you should use: UNION ALL
Like:
select name from project_content
UNION ALL
select name from project_master
UNION ALL
select name from project_master
UNION ALL
select name from project_menu_master
Just be sure, that you have the same amount of columns in each select with the same type
I have a table with text in various language. Its defined like this:
Id|Language|Text
EXAMPLE DATA
0, ENU, a
0, DAN, b
1, ENU, c
2, ENU, d
2, DAN, e
3, ESP, f
3, ENU, g
Language and Id form the key.
Now I want to extract all texts in a langauge (lets say english) and have the coorosponding text in another language (lets say danish) shown in the column next to. So the result should be:
0, a, b
1, c,
2, d, e
3, g
I know I can do a join like this:
SELECT t1.Id, t1.Text AS "ENU", t2.Text AS "DAN" table as t1
JOIN table as t2 ON (t1.Id= t2.Id)
WHERE t1.Langauge = "ENU" AND t2.Language = "DAN";
But this does not include the missing rows (ie row id=1 and id=3). How to do this?
* UPDATE ****
I get suggestion to use LEFT JOIN but I cant get it working. Maybe because my table layout is a bit different than in the simplified question above. My table is defined as this:
Language|MPageId|MFieldId|MParagraph|MText
Where Language,MPageId,MFieldId,MParagraph forms the key
I tried this:
SELECT t1.MPageId, t1.MFieldId, t1.MParagraphId, t1.MText, t2.MText
FROM main as t1 LEFT JOIN main as t2 ON (t1.MPageId = t2.MPageId AND
t1.MFieldId = t2.MFieldId AND t1.MParagraphId = t2.MParagraphId) WHERE
t1.MLanguage = 'ENU' AND t2.MLanguage = 'DAN'
SELECT t1.Id, t1.Text AS "ENU", t2.Text AS "DAN" FROM table as t1
LEFT JOIN table as t2 ON (t1.Id= t2.Id AND t2.Language = "DAN")
WHERE t1.Langauge = "ENU"
You do need the left join... but the "AND" clause for "DAN"ish would be applied AT the LEFT JOIN, and not in the WHERE clause... The where clause implies an INNER JOIN
SELECT
t1.Id,
t1.Text AS "ENU",
t2.Text AS "DAN"
from
YourTable t1
LEFT JOIN YourTable t2
ON t1.Id= t2.Id
AND t2.Language = "DAN"
where
t1.Langauge = "ENU"
You want a Left Join: http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php
select Id,
MAX(case when LanguageS='ENU' then Text else null end ) as A,
MAX( case when LanguageS<>'ENU' then Text else null end ) as B
from LAN
GROUP BY 1
Id A B
0 a b
1 c ?
2 d e
3 g f