I have three tables :
bldr_prjct,bldr_prjct_attr,bldr_prjct_attr_ref;
i want to get bldr_prjct data where bldr_prjct attributes are stored in bldr_prjct_attr.
bldr_prjct_attr_ref : In this table i have defined values all attributes related to projects :
Screenshots :
bldr_prjct
bldr_prjct_attr_ref :
bldr_prjct_attr :
My query :
SELECT
`p`.`ID`,`p`.`PRJCT_NM`,`p`.`SLUG`,`p`.`STS_CD`,
`p`.`PRJCT_GEO_LT`,`p`.`PRJCT_GEO_LG`
FROM
`bldr_prjct` `p`, `bldr_prjct_attr_ref` `pr`, `bldr_prjct_attr` `pa`
WHERE
`pa`.`REF_ID` IN (SELECT `ID` FROM `bldr_prjct_attr_ref` WHERE `PRNT_ID`=3)
Firstly, don't use implicit JOIN syntax(comma separated) , use the proper syntax of join, that will help you avoid this kind of mistakes.
You query is not working because you are missing the join relations , you may have to adjust it a bit, i guessed bldr_prjct is joined to bldr_prjct_attr_ref by id=prnt_id although you have prnt_id column in this table, so change it if needed.
SELECT `p`.`ID`,`p`.`PRJCT_NM`,`p`.`SLUG`,`p`.`STS_CD`,`p`.`PRJCT_GEO_LT`,`p`.`PRJCT_GEO_LG`
FROM `bldr_prjct` `p`
INNER JOIN `bldr_prjct_attr` `pa`
ON(`p`.id = `pa`.prnt_id )
INNER JOIN `bldr_prjct_attr_ref` `pr`
ON(`pa`.ref_id = `pr`.id and `pr`.prnt_id = 3 )
I might be missing something, but your tables don't appear to have any common reference. For instance, table bldr_prjct_attr_ref does not include any field pointing to the relevant record in table bldr_prjct. If my understanding is correct, you will have to alter these tables to allow some time of cross-referencing. For instance, add to table bldr_prjct_attr_ref a field (column) bldr_prjct_ID that points to the corresponding record in table bldr_prjct.
There is one field in the third table that appears to reference a record in the first, but when you issue a SELECT of three tables and one of them returns an empty result, then the overall select returns empty.
Related
I have sql query like this
SELECT * FROM phlegm WHERE JOIN mucus ON phlegm.id = mucus.id JOIN snot ON phlegm.id = snot.id
The problem is those tables contain several columns with identical names.
For example all 3 tables contain the column named test
If I retrieve the result of the query in PHP, then I will only get one value named test ($query->get_result()->fetch_object()->test;), because the other two get overwritten.
Is there some way to edit that query so that it adds a prefix to all columns from a table? For example, column test from table mucus would be referenced in the query as mucus_test and column test from phlegm would be phlegm_test.
One way would be doing
SELECT phlegm.test as phlegm_test, mucus.test as mucus_test FROM phlegm...
But I have a LOT of columns and tables and it would make the query longer than the Great Wall of China if I had to name each field one by one.
So is there some way to add the prefix en masse?
SELECT *, phlegm.test as phlegm_test, mucus.test as mucus_test FROM phlegm...
Used aliasing to retrieve all values associated from all three tables. if you want to reference only specific column do so by using the alias_name.column_name instead of p.*, where * means all columns belonging to table that the alias is associated with( ie. p refers to phlegm).
SELECT p.*, m.*, s.*
FROM phlegm p
JOIN mucus m ON p.id = m.id
JOIN snot s ON p.id = s.id;
I removed the WHERE from your original query above, not sure why it was there.
I have 3 tables that I'd like to pull data from and use the result set to create a new table. Note that each of these tables have identical column names.
CREATE TABLE smsout_32020Nov2014
AS
(SELECT *
FROM smsout17nov2014b,smsoutnov32014,smsout
WHERE smsoutnov32014.shortcode = 32020
AND smsout.shortcode = 32020
AND smsout17nov2014b.shortcode = 32020);
Problem is that I am getting an error that there are duplicate column names
Is there a work around?
As described in comments on your question, it is unclear what results you are actually trying to obtain. Supposing that the question reflects a complete misunderstanding of join operations, however, it may be that UNION ALL is what you're actually looking for. In particular, if you want all the rows of the three named tables for which the shortcode column has the value 32020, then that would be this:
CREATE TABLE smsout_32020Nov2014 AS (
SELECT smsout17nov2014b.*
WHERE shortcode = 32030
UNION ALL
SELECT smsoutnov32014.*
WHERE shortcode = 32030
UNION ALL
SELECT smsout.*
WHERE shortcode = 32030
)
On the other hand, if the results you are selecting are in fact in the form you want, as you have revised your question to say, then you have no alternative but to assign explicit column names to ensure column name uniqueness (so at least for every column in two of the three base tables). You can do this via aliases in the SELECT statement or via a column list in the outer CREATE TABLE statement (or both). Your original question seemed to say that the workaround you wanted was a way to avoid doing that, but now it just seems to say that you want to fix the error.
It will be a bit simpler to do the patch up in the SELECT statement. Based on your revised starting query, that would be something like this:
CREATE TABLE smsout_32020Nov2014 AS (
SELECT
smsout.*,
m.col1 AS month_col1, m.col2 AS month_col2, ... m.shortcode AS month_shortcode,
c.col1 AS code_col1, c.col2 AS code_col2, ... c.shortcode AS code_shortcode,
FROM
smsout17nov2014b m,
smsoutnov32014 c,
smsout
WHERE smsoutnov32014.shortcode = 32020
AND smsout.shortcode = 32020
AND smsout17nov2014b.shortcode = 32020
);
The columns of your new table will be col1, col2, ... shortcode, month_col1, month_col2, ... month_shortcode, code_col1, code_col2, ... code_shortcode.
Note, by the way, that even if those results are in the form you want (which I find surprising), I have trouble believing that the rows are what you want.
What do I have now is a two tables in MySQL db, one thing happened during converting tables (forum convert), and now I've an issue with encoding. I want to fix that by joining one table to another, according to ids, and ignore first table text column while replacing it by text column from other table.
Both tables have "topic_id" and "threadid" which uses same numbers to identify thread name.
They are also have "title" and "topic_title". There is some amount of other columns, ask if you will need and I post the other ones.
So, is it possible to check while "topic_id == threadid", and replace "topic_title" with "title" using MySQL query or not?
UPDATE phpbbf_topics t1
JOIN vb_thread t2 ON t1.topic_id = t2.threadId
SET t1.topic_title = t2.title
Something like this should do it.
yes it is possible try this query
Update tbl1 A SET A.topic_title = B.title
LEFT JOIN tbl2 B ON A.topic_id = B.threadid
I wonder if any can help me understand something I'm trying to solve.
I'm working on a wordpress site but this is more a sql question as I'm just querying to get some results within a template file.
I have a gallery of pictures which are advert boxes, and I need to pull these in relation to a supplied movie name, to do this Im using some custom fields on the ad pic called 'adlink' (link off ad) and ad
I'm using the nextgen gallery plugin and querying those tables, and I have three tables in total that contain the data I need to query.
ngg_pictures, nggcf_field_values & nggcf_fields.
the nggcf tables are custom fields tables,
I have got so far I can get what I need in two seperate queries, but I can't combine these into one query as it means querying the nggcf_field_values table twice, which I can't seem to sort.
I have hardcoded the search criteria in for the mo, but the 'close-encounters' bit would be a passed var, and the '156' would be the pid from the first query.
SELECT `eg_ngg_pictures`.`filename`, `eg_nggcf_field_values`.`fid`, `eg_nggcf_field_values`.`pid`
FROM eg_ngg_pictures, eg_nggcf_field_values
WHERE ((`eg_nggcf_field_values`.`field_value` LIKE 'close-encounters') AND (`eg_nggcf_field_values`.`pid` = eg_ngg_pictures.pid))
SELECT `eg_nggcf_field_values`.`field_value`
FROM eg_nggcf_field_values, eg_nggcf_fields
WHERE ((`eg_nggcf_fields`.`field_name` = 'adlink') AND (`eg_nggcf_fields`.`id` = eg_nggcf_field_values.fid) AND (`eg_nggcf_field_values`.`pid` = '156'))
any help would be greatly appreciated, I can get the results with what I have, but I like to understand how to combine these two and write better SQl. Thanks MRO
After looking at the Wordpress extension, I think the eg_nggcf_fields is the table that contains the name for a custom field. The eg_nggcf_field_values table contains the values of that custom field per picture.
So if you're looking for two fields called moviename and adlink, you have to look up two rows in the field_values table. You can join a table twice if you give it a different alias:
select pic.filename
, pic.pid
, fv1.field_value as MovieName
, fv2.field_value as Adlink
from eg_ngg_pictures pic
inner join -- Find ID for the field called 'moviename'
eg_nggcf_fields f1
on f1.field_name = 'moviename'
inner join -- Find value for field moviename for this picture
eg_nggcf_field_values as fv1
on fv1.pid = pic.pid
and fv1.fid = f1.fid
inner join -- Find ID for the field called 'adlink'
eg_nggcf_fields f2
on f2.field_name = 'adlink'
inner join -- Find value for field adlink for this picture
eg_nggcf_field_values as fv2
on fv2.pid = pic.pid
and fv2.fid = f2.fid
where fv1.field_value like 'close-encounters'
First of all, I'd recommend sticking to modern ANSI syntax for JOINing tables, which means using the JOIN clause.
Instead of using:
FROM table1, table2 WHERE table1.id = table2.pid
use:
FROM Table 1 JOIN table2 ON table1.id = table2.id
For simplicity's sake, I'd also recommend you to alias tables, as that tends to make the code more readable. Instead of having to write out egg_ngg_pictures every time, you can simply refer to the alias you assign it instead.
Lastly, when you use a LIKE operator, you usually add a wild-card character (typically %. I.e. LIKE '%123' or LIKE '123%'). You seem to look only for complete matches, which means you can just stick to using =, as that should give you slightly better performance.
Now to rewrite your query, I'd use something like the following:
SELECT
pic.filename
, fieldval.fid
, fieldval.pid
, fieldval.field_value
FROM
eg_ngg_pictures pic
JOIN eg_nggcf_field_values fieldval ON fieldval.pid = pic.pid
JOIN eg_nggcf_fields fields ON fields.id = fieldval.fid
WHERE
((fieldval.field_value = 'close-encounters')
AND fields.field_name = 'ad_link'
Note that I am not able to test the query, as I do not have your schema. But by incorporating the two queries into a single query, the join on the field_Values.PID retreieved with the 'close_encounters' value should already exist.
If the query does not work, feel free to create a SQL fiddle with the relevant tables and some data, and I'll try and get it to work with that.
Suppose that we have, in mysql, a table "mytable"
with the columns: id, title, type.
Is it possible to use its column as a table name in the same query?
For example:
SELECT m.id, m.title FROM mytable m INNER JOIN m.type WHERE m.id=2
Where "type" will give me the name of the table to do the inner join.
No, Sorry :(
The closest you can get (afaik) is to cursor through your main table and write dynamic html for the join for each row. VERY slow.
Or, find a new design pattern - Is there scope for you to post a question about what you are trying to achieve and how people may go about that?
Why not put all of your type data in a single table ? and then partition the table by type.