I have a table with the following field idd. It is however screwing up my code as I have to rename everything from array['id'] to array['idd']. Is there anyway to fix this? I tried the following!
select * from table where (idd as id) = 2
select *,idd as id from table where id = 2
Try this one
SELECT column1, column2, ..., idd AS id, columni, columni+1, ...
FROM yourTable
WHERE id=2
Related
Here's are very simplified versions of my tables.
[stock_adjust]
id
batch_table_name
batch_id
adjustment_reason
qty
[ingredient_batch]
id
batch_name
...lots of other columns
[product_batch]
id
batch_name
...lots of other columns
[packaging_batch
id
batch_name
...lots of other columns
stock_adjust contains the name of the correct batch table in the column batch_table_name which will either be "ingredient_batch", "product_batch" or "packaging_batch". I need to get the values from the correct batch table for each entry. The pseudo code for this would look something like the following:
SELECT sa.id, sa.adjustment_reason, sa.qty, batch.batch_name
FROM stock_adjust AS sa, [sa.batch_table_name] AS batch
WHERE sa.batch_id=batch.id
I have tried to simplify the description and tables as much as possible, hopefully I haven't simplified it too much and the above makes sense?
I have found several questions regarding similar issues to the following, but they either do not work correctly in this situation or I am not understanding the question correctly.
While I would recommend updating your database schema, here's an approach that could work for you given your scenario:
select sa.id, b.batch_name
from stock_adjust sa join (
select id, batch_name, 'ingredient_batch' table_name from ingredient_batch
union all
select id, batch_name, 'product_branch' table_name from product_branch
union all
select id, batch_name, 'packaging_batch' table_name from packaging_batch
) b on sa.batch_table_id = b.id and b.table_name = sa.batch_table_name
SQL Fiddle Demo
Maybee a dynamic query could be the solution. Check this answer SELECT * FROM #variable
I need to add '000' to each row in the id colum of a Wordpress database. Those are the querys that I tried:
UPDATE wp_posts SET ID = 000 + ID
UPDATE wp_posts SET ID = '000' + ID
There is no execution error but this just doesn't modified the column.
How can I do it?
From the comments I can see you want to merge 2 tables, but the ID is the problem for you. Try using union like this
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
Union all should connect the tables even with duplicate values
You can try with using below query but please take a backup for safety before trying this query.
UPDATE wp_posts SET ID = CONCAT('000', ID);
Hope, This will work for you.
I need help :( I have table like this...
ID|code|item|user|
01|aaaa|1111|0001|
02|bbbb|1111|0001|
03|cccc|1111|0001|
04|dddd|1111|0001|
05|aaaa|1111|0002|
06|eeee|1111|0002|
07|ffff|1111|0001|
I'm user 0002 and I know my item numer (for example 1111). I don't know other users, ids and other codes, but i have to get only 02,03,04,07 results (for this example). Any sinle and duplicated rows (for code column) with user 002 should be ignored... if you know what i mean. Any ideas how? :(
This can be done with a subquery filter
select *
from myTable
where code not in (
select code
from myTable
where user = '0002'
)
Try with:
select ID
from myTable
where user <> '0002'
group by ID, code
having count(code) = 1
I need to select all the values of table 1 from the first database that are not present in table 2 from the second database. I tried the code below, but DISTINCT does not work:
select DISTINCT(affected_ci),ci_name from sitequota.incidents,appwarehouse.ci_table where incidents.affected_ci <> ci_table.ci_name
DATABASE1: APPWAREHOUSE
TABLE1: CI_TABLE
COLUMN: CI_NAME
DATABASE2: SITEQUOTA
TABLE2: INCIDENTS
COLUMN: AFFECTED_CI
You could try something like:
SELECT ci_name
FROM appwarehouse.ci_table
WHERE ci_name NOT IN
(SELECT affected_ci FROM sitequota.incidents
)
Totally out of ideas here, could be needing a simple solution.
Basically my desired query is :
SELECT * FROM table WHERE id = 3,4
I want to select only the row which has ID 3 and 4, or maybe name "andy" and "paul"
Thank you very much for the answer
Try or:
WHERE id = 3 or id = 4
Or the equivalent in:
WHERE id in (3,4)
Try this -
select * from table where id in (3,4) or [name] in ('andy','paul');