I have the database structure as shown in the below image:
I want to retrieve variant_id that belongs to particular product_id
Example :
lets say 1 and Size = L , Color = Green.
I expect mysql query to return variant_id = 7.
What is the expected query to be used in this scenario?
You can use following query.
I'm assuming your table name is table1
select variant_id from (select * from table1 where
product_id=1 and ((attribute_name='Size' and value='L')
or (attribute_name='Color' and value='Green'))) as temp
group by variant_id having count(*)>1
Related
I want make a leaderboard from my database, I already make a code but the sql says error
"SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun)
MySQL said: Documentation
#1242 - Subquery returns more than 1 row"
heres my sql query :
SET #a1 = (SELECT SUM(d.poin_diskusi)
FROM diskusi d
GROUP BY d.id_akun);
SET #a2 = (SELECT SUM(j.nilai_jawaban)
FROM jawaban j
GROUP BY j.id_akun);
SET #hasil = #a1 + #a2;
SELECT #hasil
Thank you !
this is an example data in table jawaban:
this is an example data in table diskusi:
and my desired answer:
where tera123 have 135 TOTAL(d.poin_diskusi+j.nilai_jawaban) and david123 have 90 TOTAL(d.poin_diskusi+j.nilai_jawaban)
the point is to show TOTAL for every user
It looks to me (without looking at your images; if you want to show information, please provide it in text) like you want something like:
select id_akun, sum(point_value) as total_points from (
select id_akun, sum(poin_diskusi) as point_value
from diskusi group by id_akun
union all
select id_akun, sum(nilai_jawaban) as point_value
from jawaban group by id_akun
) as point_values
group by id_akun
The problem are the GROUP BY keywords. Inside the images you provided with example data, one can see that there are multiple entries for the columns jawaban.id_akun and diskusi.id_akun. Because of that, both SELECT will return multiple rows as result, for each different value of the id columns one summed up row. Try it without the GROUP BY clause.
Table to store array of Images:
`products` and `products_photos`
`products` includes only `id` field
`products_photos` includes `product_id` and `filename`
Table values:
products
--------
id-6
id-7
products_photos
---------------
6 - filename1
6 - filename2
7 - filename3
In these two table id 6 includes 2 images but i need to fetch only one image from that. How to write a query for that in mysql or laravel.
If you want to get the latest (last added) image of the product then you can do the following way
NOTE : Instead of *, use the precise columns so that your query performs faster.
SELECT * FROM products_photos WHERE product_id = 6 ORDER BY products_photos.id DESC LIMIT 1;
If you want to get the first (old added) image of the product then you can do the following way
SELECT * FROM products_photos WHERE product_id = 6 ORDER BY products_photos.id ASC LIMIT 1;
Updated : Query to get the list of all the records group
SELECT MAX(id) as id, photo_name FROM products_photos GROUP BY photo_name
In case if you have more than one columns and want to get the latest record for that too then go for this
Eg. Considering that your having description column and want to get the MAX (latest) content than use MAX, if you need oldest use MIN
SELECT MAX(id) as id, photo_name, MAX(description) as description FROM products_photos GROUP BY photo_name
You can give limit to your query to get single result
SELECT * FROM products_photos WHERE product_id = 6 ORDER BY id LIMIT 1;
Which will give single image only for id = 6 product
For retrieve all data use group by
SELECT * FROM GemSellerPortal.products pr
LEFT JOIN GemSellerPortal.products_photos ph
ON ph.product_id = pr.id
GROUP BY pr.id
I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);
What i want, to display rows from a table which is selected by a field from other table single value, lets say to display images from a table by last category id.
I have this type of query, but this return me all matching keys rows, if i inset LIMIT 1 then it return one row...
SELECT i.prof_image FROM profile_images i
JOIN images_cat cat ON (cat.cat_id = i.cat_id)
GROUP BY i.prof_image;
//OR LIMIT 1;
Any idea to fix this problem. (i.e. displaying the latest category images)?
This will work for your specific example.. If you need to be more selective, then please post some more details..
SELECT i.prof_image
FROM profile_images i
WHERE cat_id = (select max(cat_id) from images_cat)
SELECT * FROM table_1
LEFT JOIN table_2 ON table_1.id = table_2.id
This query will grab all things in table_2 that have the same id value.
Note that it is a LEFT JOIN - which means that if there are no matching values in table_2, it will still return the values from table_1.
What is your intent with using last()?
Hope this helps.
Any idea how to copy: name, content from rows where language_id = 1 to rows where language_id = 2?
How should SQL command look like?
I want to achive:
http://dev.mysql.com/doc/refman/5.0/en/insert-select.html is what you need to do
assuming it is the productid that you want to update from lang1 to lang 2
update a set
a.name = b.name,
a.content = b.content
from tablea a
join tablea b on a.productid = b.productid
where a.language_id = 2
and b.language_id = 1
ofcourse this will do it for every row in the table so if you want to restrict it then make sure to restrict it by the productids
Did you mean copying all language_id=1 rows to language_id=2 ones?
My knowledge of MySQL syntax is very poor, so I dare not give you all the codez, but at least you may find the following approach useful:
Create a temp table with the structure like this:
product_id int,
name (varchar?)
content (varchar?)
That is, include product_id and all the columns you need to copy.
Populate the temp table with the language_id=1 data. Probably like this:
INSERT INTO temp_table
SELECT product_id, name, content
FROM orig_table
WHERE language_id = 1
Update those rows in the original table where language_id=2 with the corresponding data in the temp table. It may look like this:
UPDATE orig_table
SET
name = temp_table.name,
content = temp_table.content
FROM temp_table
WHERE orig_table.product_id = temp_table.product_id
AND orig_table.language_id = 2
Insert the rows from the temp table into the original table, where the products don't have language_id=2. Something like this:
INSERT INTO orig_table (product_id, language_id, name, content)
SELECT product_id, 2, name, content
FROM temp_table
WHERE NOT EXISTS (
SELECT 1 FROM orig_table
WHERE product_id = temp_table.product.id
AND language_id = 2
)
If you didn't mean to change the already existing language_id=2 data, then step #3 should be omitted and you might further want to modify step #2 in such a way that it selected language_id=1 data only for the products lacking language_id=2.