MySQL Join not Producing Desired Results - mysql

I'm working on writing a snippet for ModX that will find all document with the specified TV set to a user submitted value.
Here is a description of the tables I'm working with.
http://wiki.modxcms.com/index.php/Template_Variable_Database_Tables
Here is my query:
SELECT contentid
FROM prefix_site_tmplvar_contentvalues
JOIN prefix_site_tmplvar_contentvalues
ON prefix_site_tmplvars.id = prefix_site_tmplvar_contentvalues.tmplvarid
WHERE value="Red"
Currently it's producing results such as this:
http://pastebin.com/mEJ1w2be
Where each document ID will have a new row in the results for each Template Variable. So, for 7455 in the example there will be one array for the color="red" one for material="wood" one for size="small". Which, makes it difficult if I want to find a product that is red, small, and made of wood.
Is there a way that I could join these tables so that I could get one row per product with the document id and a set of template variable with associate values—not all broken up?

try
GROUP BY contentid
this will smush all the rows with the same contentid together.

Related

Add additional columns and subqueries to a MySQL VIEW to create more readable version of an existing table

This problem is really causing me a headache and I have spent hours for different solutions to my problem, but no luck so far.
At our online survey software ("Limesurvey") all data gets stored at a flat table. There is one row for each response data set and each question item has a related column at that table. The actual responses are stored at the table cells. The thing is that this data is hard to read because:
For array questions Limesurvey just stores the answer code -> we also want to add a column for the full answer text.
For multiple numeric questions Limesurvey stores the number inputted -> we also want to add a column listing the related sub-question text.
We are trying to achieve the above by creating a MySQL VIEW based on the table definition of the survey's response table. For that we have extended our VIEW feature to add additional columns when creating the VIEW and we have added code to query the related answer and sub-question texts. Those queries work fine, this is what the VIEW shows if ONE data set is inputted (looks good so far):
One data set
Thing is that once we add a second data set with different answers, the data gets mixed up. It looks like MySQL just aggregates the data from all rows to fill the additional columns. There are now additional answer/sub-question texts at both rows (marked "?") though for that data set no answer was given, see:
Two data sets
My main question is: Is it possible to kind of define the scope for the additional "..._TEXT" columns so they get filled based on details of the current row, not the whole data?
Let me know if you have any questions, it is a little complex to describe this issue.
This is how I would do it. Using joins like this you can see that you are missing a relationship between the main select and the question columns. See the comments in my code below.
CREATE VIEW lime_view_967824 AS
SELECT
ls.id 'id',
ls.submitdate 'submitdate',
ls.lastpage 'lastpage',
ls.startlanguage 'startlanguage',
ls.967824X1087X6950SQ001 'arraySD_SQ001',
COALESCE(a1.answer,'') AS 'arraySD_SQ001[TEXT]',
ls.967824X1087X6950SQ002 'arraySD_SQ002',
COALESCE(a2.answer, '') AS 'arraySD_SQ002[TEXT]',
ls.967824X1087X6950SQ003 'arraySD_SQ003',
COALESCE(a3.answer, '') AS 'arraySD_SQ003[TEXT]',
ls.967824X1087X6946SQ001 'multiOptions_SQ001',
ls.967824X1087X6946SQ002 'multiOptions_SQ002',
ls.967824X1087X6946SQ003 'multiOptions_SQ003',
ls.967824X1087X6946other 'multiOptions_other',
ls.967824X1087X6941SQ001 'multiNumInput_SQ001',
COALESCE(q1.question,'') AS 'multiNumInput_SQ001[TEXT]',
ls.967824X1087X6941SQ002 'multiNumInput_SQ002',
COALESCE(q2.question,'') AS 'multiNumInput_SQ002[TEXT]',
ls.967824X1087X6941SQ003 'multiNumInput_SQ003',
COALESCE(q3.question,'') AS 'multiNumInput_SQ003[TEXT]',
ls.967824X1087X6941SQ004 'multiNumInput_SQ004',
COALESCE(q4.question,'') AS 'multiNumInput_SQ004[TEXT]'
FROM lime_survey_967824 ls
LEFT JOIN lime_answers a1 ON a1.qid=6950 AND a1.language='en' AND ls.967824X1087X6950SQ001=a1.code
LEFT JOIN lime_answers a2 ON a2.qid=6950 AND a2.language='en' AND ls.967824X1087X6950SQ002=a2.code
LEFT JOIN lime_answers a3 ON a3.qid=6950 AND a3.language='en' AND ls.967824X1087X6950SQ003=a3.code
LEFT JOIN lime_questions q1 ON q1.qid=6941 AND q1.title='SQ001' AND q1.language='en' AND q1.type='K' -- no join to ls?
LEFT JOIN lime_questions q2 ON q2.qid=6941 AND q2.title='SQ002' AND q2.language='en' AND q2.type='K' -- no join to ls?
LEFT JOIN lime_questions q3 ON q3.qid=6941 AND q3.title='SQ003' AND q3.language='en' AND q3.type='K' -- no join to ls?
LEFT JOIN lime_questions q4 ON q4.qid=6941 AND q4.title='SQ004' AND q4.language='en' AND q4.type='K' -- no join to ls?

Append string to another table in MySQL

I’ve got a problem selecting some things in my database.
I’ve got 1 database with a lot of tables, however I only use 2 with this specific task.
I have a Clothing table, and I need to import this in my new database. I have succesfully transferred a lot of data, but now I need to take 1 final small step into completing this. In my Clothing table I have a column called Youtube link, here I have a link with an Youtube link, this specific link, in this specific column, I want to append that to another table I have in the database, let’s call it new_clothing_table. I there have a description column called prod_desc and I want to append the Youtube link to that column.
But there is also another “problem”, it’s not that every product has a Youtube link, so things have to be filtered in order to not fuck things royally up. A advantage I have, I have in both tables a product_name, and these are all the same. So I want to transfer that specific Youtube link if it’s there (sometimes there is a 0 filled in or nothing, but I dont think it’s NULL because if I make a SELECT query where Youtube_link is null I get zero rows..)
So can someone help me out>?
mysql has an update-join construct you can use:
UPDATE new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'
SET nct.description = CONCAT(nct.description, c.youtube_link)
EDIT:
To make sure this does what you want, you could first select the updated content in order to examine it:
SELECT nct.description AS old_description,
CONCAT(nct.description, c.youtube_link) AS new_description
FROM new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'

Is this a good way of copying data from one table to another?

I have two tables, links1 and links2. Links1 one has several million rows with the following columns: ID, URL, UTitle and UDesc. The URL column holds urls from around the web, and the UTitle and UDesc columns are empty.
Links2 also has several million rows with the following columns: ID, URL, title and description. The URL column has urls, but most of them are different ones than the ones in links1. The title and description columns are filled with the title and meta:description for the url in each row. I'm now trying to compare the two tables, and whnever one of the urls in link2 corresponds to a url in links1, I want to copy the data in title and description (in links2) to UTitle and UDesc (in links1).
I'm still new at Mysql, so I'm not sure the following is the fastest/best way to do this. I'd appreciate your input if there is a better/faster way.
UPDATE links1
INNER JOIN links2 ON (URL.value = URL.value)
SET links1.UTitle = links2.title, links1.UDesc = links2.description
Thanks!
It is faster way with one query, but you have a mistake in your query. Here is the corrected version:
UPDATE links1
INNER JOIN links2 ON (links1.URL = links2.URL)
SET links1.UTitle = links2.title, links1.UDesc = links2.description

inner query of subqery returning multiple rows

I am not that experience in sql so please forgive if its not a good question to ask,but i researched around almost for 3-4 days but no able to solve.
My problem is i have a table which have multiple image names in it,so what i have to do is whoever is the follower of a particular user i have to get the imaged from this table,so one user there can be multiple followers,so i have to fetch the images posted by all the followers.
Here is the subquery code snippet i am using.
SELECT id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email=ANY(SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com')
So the inner query here returns multiple values,for each value(being_followed) i have to fetch all the images and display it,but with this query each time i get only one image.I tried IN also but didnot work out.
Table structure:-
Outfitpic_list table
id|outfit_image|datetime|Email|image_title|description
Follower_table
bring_followed|follower
Please help,I am stuck..!!
Thank you..!!
I think your problem may be the = sign between "E-mail" and "Any". Try this statement:
SELECT
id,
outfit_image,
img_title,
description
FROM outfitpic_list r2
WHERE Email IN
(
SELECT being_followed
FROM follower_table
WHERE follower='test#gmail.com'
)
It's the same statement, without the = sign, and the ANY keyword replaced with IN. (I cleaned it up a little to make it more readable)

Relational Database simple search string

Hey guys,
This is a follow-up to a question that I asked earlier. It is my first time using a relational database and I need help with a quick search string to bring up desired results.
Background information: I'm making a database for my photo portfolio and want to be able to retrieve image links/data via their categories. Each image can be listed in multiple categories.
My database is set-up as follows :
TABLE tbl_images (image_id, image_title, image_location, image_descrip,image_url)
TABLE tbl_categories (category_id,category_name,category_descrip)
TABLE tbl_image_categories (image_id,category_id)
Where one of my images (image_id=1) has two categories (Desert [category_id=1] and Winter [category_id=2]). Which I defined in tbl_image_categories as 1,1 and 1,2.
I also have a few other images that I defined as Desert images [category_id=1].
How would I go about getting which images should be loaded based on the Desert Category?
I tried:
SELECT tbl_images.image_url
FROM tbl_images,
tbl_image_categories,
tbl_categories
WHERE tbl_categories.category_id = 1
Try this:
SELECT DISTINCT tbl_images.image_url
FROM tbl_images,
tbl_image_categories,
tbl_categories
WHERE chad_categories.category_id = 1 //category_id=1 for Desert
AND chad_images.image_id = chad_image_categories.image_id
AND chad_image_categories.category_id = chad_categories.category_id